Pass Data From One Activity to Another Using Intent in Xamarin

Introduction

 
In this article, we will see how to data from one activity to another using Intent in Xamarin. In my previous article, we learned about the Checkbox widget in Xamarin.
 
Let's begin.
 
1. Create a new Android Application in Visual Studio / Xamarin.
 
image1
 
2. Add a layout (named Main.axml).
 
image2
 
3. Drop a TextView, EditText(set id="@+id/txt_Name") and Button(set id="@+id/btn_Submit" and text="Submit") control onto the Main.axml Layout.
 
image3
 
4. Add another Layout and name it Result.axml.
 
image4
 
Drop a TextView (Large) control onto the Result.axml Layout and set id="@+id/txt_Result".
 
image6
 
Activity1.cs Code 
  1. using System;    
  2.     
  3. using Android.App;    
  4. using Android.Content;    
  5. using Android.Widget;    
  6. using Android.OS;    
  7.     
  8. namespace IntentDemo    
  9. {    
  10.     [Activity(Label = "IntentDemo", MainLauncher = true, Icon = "@drawable/icon")]    
  11.     public class Activity1 : Activity    
  12.     {    
  13.         EditText txt_Name;    
  14.         Button btn_Submit;    
  15.         protected override void OnCreate(Bundle bundle)    
  16.         {    
  17.             base.OnCreate(bundle);    
  18.             // Set our view from the "main" layout resource    
  19.             SetContentView(Resource.Layout.Main);    
  20.             //Get txt_Name and btn_Submit Button CheckBox control from the Main.xaml Layout.      
  21.             txt_Name = FindViewById<EditText>(Resource.Id.txt_Name);    
  22.             btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);    
  23.             //btn_Submit click event    
  24.             btn_Submit.Click += btn_Submit_Click;    
  25.         }    
  26.     
  27.         void btn_Submit_Click(object sender, EventArgs e)    
  28.         {    
  29.             //if EditText in not Empty    
  30.             if(txt_Name.Text!="")    
  31.             {    
  32.                 //ing the Activity2 in Intent    
  33.                 Intent i = new Intent(this,typeof(Activity2));    
  34.                 //Add PutExtra method data to intent.    
  35.                 i.PutExtra("Name",txt_Name.Text.ToString());    
  36.                 //StartActivity    
  37.                 StartActivity(i);    
  38.             }    
  39.             else    
  40.             {    
  41.                 Toast.MakeText(this,"Please Provide Name",ToastLength.Short).Show();    
  42.             }    
  43.         }    
  44.     }    
  45. }   
In the preceding code, we have created an Intent and bound the Activity2 to it. The PutExtra method of Intent allows us to store data in Key-Value pairs. We can retrieve the data in the other Activity using the Intent.GetTypeExtra method. In the preceding code, we have ed a string using a PutExtra() method and for retrieving the string in the other activity (in other words Activity2), we use the Intent.GetStringExtra("Name") method and a key/value pair in it as a parameter.
 
Activity2.cs Code
  1. using Android.App;    
  2. using Android.Content;    
  3. using Android.OS;    
  4. using Android.Widget;    
  5.     
  6. namespace IntentDemo    
  7. {    
  8.     [Activity(Label = "Result")]    
  9.     public class Activity2 : Activity    
  10.     {    
  11.         protected override void OnCreate(Bundle bundle)    
  12.         {    
  13.             base.OnCreate(bundle);    
  14.             // Set our view from the "Result" layout resource    
  15.             SetContentView(Resource.Layout.Result);    
  16.             //Get txt_Result TextView control from the Main.xaml Layout.      
  17.             TextView txt_Result = FindViewById<TextView>(Resource.Id.txt_Result);    
  18.             //Retrieve the data using Intent.GetStringExtra method    
  19.             string name = Intent.GetStringExtra("Name");    
  20.             txt_Result.Text ="Hello, "+name;    
  21.         }    
  22.     }    
  23. }   
Final Preview 
 
image6
 
I hope you like it. Thanks.