As we’re working on MyBookStore Android App, we have reached to a point where we can navigate from one Activity to another Activity. We need to specify a particular Activity to which we need to navigate to and this is known as Explicit Intent. This means, we need to explicitly define which activity we need to go to. There is another type of intent called Implicit Intent which I’ll discuss in a separate post in future in this series.
Let’s get started with this post. Earlier we had only one activity named MainActivity, while in order to be able to navigate to a new activity, we need to add more activities. For our purpose, we’ll add three new activities namely AboutAppActivity, ViewBookActivity, and ViewCartActivity. We’re keeping the app simple, but in a complex application, you can add as many activities as you like.
To add a new activity, you need to right click MyBookStore project, click Add, and select Class from the menu.
This will open up Add New Item window, from where you can select C# -> Android -> Activity and name your activity as required. It’s better to use Activity at the end of the selected name like ViewCartActivity.cs, instead of just ViewCart.cs.
You can also change the Label of your activity which will be shown when the activity is displayed on screen in the Android.
Now, it’s time to find controls from the activity view in the onCreate method. In our case, we need to find the buttons, so we could click those and then navigate to the required activities. We’ll first declare private variables and then find the views using FindViewById method.
Once we have found the controls from the view, we need to attach the click events to these buttons.
Now we need to add the code to create a new explicit intent and then start the new activity.
When you click on View Books button the following new activity will be displayed on screen.
If we look at the code for the MainActivity from where we’ll navigate to other activities, this is how it should look like.
[Activity(Label = "MyBookStore", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
private Button buttonViewBooks;
private Button buttonViewCart;
private Button buttonAboutApp;
protected override void OnCreate(Bundle bundle)
Log.Info("MyBookStore", "This is info message");
// Set our view from the "main" layout resource
buttonViewBooks = FindViewById<Button>(Resource.Id.buttonViewBooks);
buttonViewCart = FindViewById<Button>(Resource.Id.buttonViewCart);
buttonAboutApp = FindViewById<Button>(Resource.Id.buttonAboutApp);
buttonViewBooks.Click += ButtonViewBooks_Click;
buttonViewCart.Click += ButtonViewCart_Click;
buttonAboutApp.Click += ButtonAboutApp_Click;
private void ButtonAboutApp_Click(object sender, System.EventArgs e)
var intent = new Intent(this, typeof(AboutAppActivity));
private void ButtonViewCart_Click(object sender, System.EventArgs e)
var intent = new Intent(this, typeof(ViewCartActivity));
private void ButtonViewBooks_Click(object sender, System.EventArgs e)
var intent = new Intent(this, typeof(ViewBooksActivity));
Enjoy this post and we’ll talk in the next post soon. 🙂