Brandon Jake Torres
Brandon Jake Torres

Reputation: 11

how to pass data using intent in xamarin

i'm new in developing android in xamarin i just want to ask in how to pass data through other activity using intent ? This thing work (https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/) but i want to collect first all the data in 2 activity before they show the summary in my 3rd activity.(by the way i'm creating a registration app thanks for the future answers :) )

Upvotes: 1

Views: 3195

Answers (2)

Divyesh
Divyesh

Reputation: 2391

you can pass whole object and desterilize that in another activity like this

//To pass:
intent.putExtra("yourKey", item);

// To retrieve object in second Activity
getIntent().getSerializableExtra("yourKey");

For only single value you can use

//Method 1
string code = Intent.GetStringExtra("id") ?? string.Empty;
string name = Intent.GetStringExtra("Name") ?? string.Empty;

//OR

//Method 2

string Id = Intent.GetStringExtra("id") ?? string.Empty;
Item item = new Item();
item = itemRepo.Find(Convert.ToInt32(id));

Upvotes: 0

Rendy Del Rosario
Rendy Del Rosario

Reputation: 1297

  1. On the first activity you create the second activity by an intent and use the method PutExtra to pass the data you want with a relevant key name that you will need after starting the new activity to retrieve the data.

    var secondActivity = new Intent (this, typeof(SecondActivity));
    
    secondActivity.PutExtra ("Data", "Sample Data");
    
    StartActivity(secondActivity);
    
  2. On second activity OnCreate retrieve the data using the key name it was stored with and the correct method relevant to the data type passed. In this example as is a string by calling Intent.GetStringExtra.

    string text = Intent.GetStringExtra ("Data") ?? "Data not available";
    
  3. You can repeat 1 & 2 for the summary activity.

Upvotes: 3

Related Questions