Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pass Data From One Activity to Another Using Intent in Xamarin

DZone's Guide to

Pass Data From One Activity to Another Using Intent in Xamarin

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In 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.


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}