Pass Data From One Activity to Another Using Intent in Xamarin
Join the DZone community and get the full member experience.
Join For FreeIn this Article, you will learn how to pass data from one activity to another using Intent in Xamarin. In my previous Article, we learnt about Checkbox widget in Xamarin.
Let's Begin:
1. Create a new Android Application.
2. Add a layout (named as Main.axml).
3) Drop TextView, EditText(set id="@+id/txt_Name") and Button(set id="@+id/btn_Submit" and text="Submit") control onto the Main.axml Layout.
4) Add another Layout and name it as Result.axml.
Drop TextView(Large) control on Result.axml layout and set id="@+id/txt_Result".
Activity1.cs Code:
using System; using Android.App; using Android.Content; using Android.Widget; using Android.OS; namespace IntentDemo { [Activity(Label = "IntentDemo", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { EditText txt_Name; Button btn_Submit; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); //Get txt_Name and btn_Submit Button CheckBox control from the Main.xaml Layout. txt_Name = FindViewById<EditText>(Resource.Id.txt_Name); btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit); //btn_Submit click event btn_Submit.Click += btn_Submit_Click; } void btn_Submit_Click(object sender, EventArgs e) { //if EditText in not Empty if(txt_Name.Text!="") { //passing the Activity2 in Intent Intent i = new Intent(this,typeof(Activity2)); //Add PutExtra method data to intent. i.PutExtra("Name",txt_Name.Text.ToString()); //StartActivity StartActivity(i); } else { Toast.MakeText(this,"Please Provide Name",ToastLength.Short).Show(); } } } }
In above Code, We have created an Intent and Bind Activity2 to it. PutExtra method of Intent allows us to store data in Key-Value pairs. We can retrieve the data in the other Activity using Intent. GetTypeExtra method. In above Code, We have passed string using PutExtra() method and for Retrieving the string in another activity, we use Intent.GetStringExtra("Name") method and pass key value in it as a parameter.
Activity2.cs Code:
using Android.App; using Android.Content; using Android.OS; using Android.Widget; namespace IntentDemo { [Activity(Label = "Result")] public class Activity2 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "Result" layout resource SetContentView(Resource.Layout.Result); //Get txt_Result TextView control from the Main.xaml Layout. TextView txt_Result = FindViewById<TextView>(Resource.Id.txt_Result); //Retrieve the data using Intent.GetStringExtra method string name = Intent.GetStringExtra("Name"); txt_Result.Text ="Hello, "+name; } } }
Final Preview:
Hope you like it. Thanks.
Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
A React Frontend With Go/Gin/Gorm Backend in One Project
-
What Is Envoy Proxy?
-
From On-Prem to SaaS
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
Comments