DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Data Engineering
  3. Data
  4. Pass Data From One Activity to Another Using Intent in Xamarin

Pass Data From One Activity to Another Using Intent in Xamarin

Anoop Kumar Sharma user avatar by
Anoop Kumar Sharma
CORE ·
Apr. 01, 15 · Interview
Like (0)
Save
Tweet
Share
9.06K Views

Join the DZone community and get the full member experience.

Join For Free

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.


Intent (military) Data (computing) Pass (software) xamarin

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: