Android
iOS
 

Pass Data Between Activity

This recipe shows how to use intents to pass data between activities.

Recipe

To pass data to an activity, follow these steps.

  1. Create a new Xamarin.Android application. The project template will create a single activity named Activity1 (MainActivity.cs), which contains a button.
  2. Add a second activity class named Activity2 to the project.
  3. From the button.Click handler in MainActivity.cs, create an intent for Activity2, and add data to the intent by calling PutExtra.
button.Click += delegate {
       var activity2 = new Intent (this, typeof(Activity2));
       activity2.PutExtra ("MyData", "Data from Activity1");
       StartActivity (activity2);  
};
  1. In Activity2.OnCreate, retrieve the data by calling Intent.GetStringExtra.
string text = Intent.GetStringExtra ("MyData") ?? "Data not available";

Additional Information

Each screen in an application is represented by an activity. Sending asynchronous messages called intents, which can include data payloads, as shown in this recipe, starts activities. For more information, see the Getting Started series and the Activity Lifecycle in the Xamarin.Android documentation.