Xamarin – Opening another Activity in Android Application

Unless your application is a single page application, you need another page to do some operations.

In this post we would see, how to open another activity in an Android Application.

Here is what needed to open another activity from current activity.

  1. Two Activities
  2. Two Layouts
  3. Intent

As you see above, we are familiar with Activity and Layout.

What is Intent?

Intent is an object that provides runtime binding between separate components (such as two activities). The Intent represents an app’s “intent to do something.” You can use intents for a wide variety of tasks, but most often they’re used to start another activity. The following image briefly explains what an Intent can do.

Let’s create an application and see Intent in action.

The project is created with a Main layout and an activity as Activity1.

We would add one more Layout and an activity as well.

Add Android Layout

Right click on the Layout folder and select “Add New Item”. And from the templates select “Android Layout”.

We would keep a TextView control to display that we are in the Layout1 Activity.

Add another Activity

Right click on the project and select “Add New Item”. Then from the templates select Activity.

Add the following code, to bind the layout1 with this activity.

We need to build the project whatever we have done so far.

Come back to the Activity1.cs and do the following code.

In the above image, we are finding the button and subscribing to its click event and then inside we are creating an intent with the above parameters, and then calling the method StartActivity which takes Intent as parameter.

Now let’s run the application and see it in action.

And on “Open Activity” button click, it opens the Activity2.

Those who want to reduce their code. They could ignore the step of creating intent. Instead the overload method StartActivity would do the task for you. As follows:

Hope this post helps, thanks for reading.


Full Source Code: Click Here