Data passing in multiple screens from one activity to another using Xamarin in Android

Introduction :-
In this Article, we will see how to create multiple screens in Android using Xamarin and pass data between them.
Android applications consist of a set of loosely coupled screens, represented by Activity classes.
Since each Activity is essentially decoupled from others, there needs to be a way to launch them and optionally pass data to them. On Android this is accomplished using Intents.

Intents are the classes that describe a message, both what the desired action of the message is and a data payload to send along with it. They are commonly used within applications to launch Activities. To launch a new Activity, we create a new Intent, set the Context and the Activity class to launch and then tell the OS to handle the Intent, which launches the Activity.

Description :-
To create the app we need 2 layout screen and 2 activity.

Following are the steps to create the app

Step #1:
Adding 2 two layout files to your project named as Main.axml and SecondLayout.axml


<LinearLayout xmlns:android=""
        android:hint="Insert text to see in Second Screen" />
        android:text="Go to Second Screen" />

So now we have a textbox and a button in our Main.axml page.
The textbox is to insert data and clicking on the button, will navigate to the second screen to display the text you have entered in the first screen texbox.

SecondLayout.axml :

<LinearLayout xmlns:android=""
        android:id="@+id/TextView2" />

In this second screen there is only a textbox, which will display the data from the first screen.

Step #2
Now we need to add 2 activity. Below is the code for activities.


[Activity (Label = "First Screen", MainLauncher = true)]
	public class MainActivity : Activity

		protected override void OnCreate (Bundle bundle)
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

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

			// Get the textview from the layout resource
			TextView mainTextView = FindViewById<TextView> (Resource.Id.mainEditText);
			button.Click += delegate {

				///Create new intent to pass data
				//FirstParameter : The current Activity passed as the Context
				//SecondParameter : The type of Activity that is to be used for the intent.
				var secondActivity = new Intent (this, typeof(SecondActivity));

				//The intent putextra method takes 2 parameters
				//FirstParameter : The name of the extra data
				//SecondParameter : The value to be passed
				secondActivity.PutExtra ("FirstActivity", mainTextView.Text);

				StartActivity (secondActivity);  


[Activity (Label = "Second Screen")]			
	public class SecondActivity : Activity
		protected override void OnCreate (Bundle bundle)
			base.OnCreate (bundle);

			// Set the view from the "SecondLayout" layout resource
			SetContentView (Resource.Layout.SecondLayout);

			//The Intent GetStringExtra Parameter is used to retrieve data added to the Name
			//It takes single parameter
			//Name : The name of the desired item.
			string text = Intent.GetStringExtra ("FirstActivity") ?? "Data not available";
			TextView myData = FindViewById<TextView> (Resource.Id.TextView2);
			myData.Text = text;

Now let’s go ahead and build the app to run in an emulator. Let’s put some data on the first screen and click the button. We can find the text in the second screen.

Hope this helps !!!

Written By: Anup Samantaray, Software Developer, Mindfire Solutions

One thought on “Data passing in multiple screens from one activity to another using Xamarin in Android

Leave a Reply

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

You are commenting using your 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