[Xamarin – Android] Work With Multi Activity

This is simple example how to work with multi activity. We have MainActivity in default, and need to make another activity. Right click on your project, select menu “Add” and “New File”. In left menu select “Android” and select “Android Activity” in center menu. You can set make a new layout for that activity with add new layout in “Resources-layout-yourNameLayout.axml”. You can call that layout with edit onCreate function in your new Activity :

         protected override void OnCreate (Bundle bundle)
             base.OnCreate (bundle);
             SetContentView (Resource.Layout.yourNameLayout); // this is code to set you layout

From previous post application (https://sabitlabscode.wordpress.com/2014/01/13/xamarin-android-create-hello-world/), i will call second activity when we click to button in the launch activity. You can change this :

 protected override void OnCreate (Bundle bundle)
             base.OnCreate (bundle);
             SetContentView (Resource.Layout.Main);

             // Get our button from the layout resource,
             // and attach an event to it
             Button button = FindViewById<Button> (Resource.Id.myButton);

             button.Click += delegate {
                 // this is old code
                 //button.Text = string.Format ("Hello World");

                 //change like this :
                 var second=new Intent(this,typeof(YourNewActivity));  // define your new activity
                 // if you want to pass data from current activity to another activity, do this
                 // in new activity you can call with with name "data1" (like code below)
                 second.PutExtra("data1","This is data 1");  
                 // call activity

Finish.. Hope it help..
Happy coding..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: