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

Main.axml:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:inputType="textMultiLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mainEditText"
        android:hint="Insert text to see in Second Screen" />
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go to Second Screen" />
</LinearLayout>

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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="52.6dp"
        android:id="@+id/TextView2" />
</LinearLayout>

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.

MainActivity.cs:

[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);  
			};
		}
	}

SecondActivity.cs

[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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s