Hello readers, Here I am going to show you how we can Pass Data Between Activity window in Xamarin Android.

How to Pass Data Between Activity Window in Xamarin Android?

This is a most common situation to pass data between application pages. In Web applications, we have different approaches like Query String, Sessions etc to Pass Data Between Activity pages. Here I am going to show you how to pass data between Xamarin Activity. Follow the steps.

Step 1- Open Visual Studio and create Blank App called PassingDataBetweenActivity as shown in below image.

Pass Data Between Activity

Your solution explorer will look like this.

Pass Data Between Activity

Step 2- Goto Solution Explorer>Resource>Layout>Main.xaml. Add a button to Layout. Here is the code for Main.xaml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:text="Goto Second Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnGotoSecond" />
</LinearLayout>

Step 3- Add a listener for the button that we have added in Step 2. Goto Solution Explorer and open MainActivity.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace PassingDataBetweenActivity
{
[Activity(Label = "SecondActivity")]
public class SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SecondLayout);
string sData = Intent.GetStringExtra("WelcomeText");
TextView lblWelcomeText = FindViewById<TextView>(Resource.Id.lblWelcomeText);
lblWelcomeText.Text = sData;
// Create your application here
}
}
}

Step 4- Now add another Activity called SecondActivity. For this Right Click on Solution Explorer and choose Add New Item.

Pass Data Between Activity

Step 5- Goto Solution Explorer>Resource and right click on Layout folder and Add a new Layout called SecondLayout.

Pass Data Between Activity

Step 6- Goto SecondLayout.axml file and write the following code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblWelcomeText" />
</LinearLayout>

As you can see we have added a TextView that will show the data from MainActivity.

Step 7- Open SecondActivity.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace PassingDataBetweenActivity
{
[Activity(Label = "SecondActivity")]
public class SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SecondLayout);
string sData = Intent.GetStringExtra("WelcomeText");
TextView lblWelcomeText = FindViewById<TextView>(Resource.Id.lblWelcomeText);
lblWelcomeText.Text = sData;
// Create your application here
}
}
}

Where,

Intent.GetStringExtra(“WelcomeText”) is used to get parameter value from first activity.

Now run the application. Your output window will look like this.

Pass Data Between Activity

Click on the button.

Pass Data Between Activity

Download Source Code here.

View More:

Conclusion:

I hope this would be the helpful tutorial for you. Your feedback, Comments, and suggestion are always welcome.

Thank You.