简体   繁体   中英

Pass Intent from one activity to another activity

I want to transfer Intent to another activity in Xamarin.Android. Basically I need Intent.Data and Intent.ClipData to another activity, so I am using the below code to transfer the Intent but I don't know the proper way to retrieve that.

Here's the code in Main Activity

Intent intent = new Intent(this, typeof(SecondActivity));
intent.PutExtra("FilesURI", 0);
intent.PutExtras(intent);
StartActivity(intent);

In SecondActivity

if (intent.HasExtra("FilesURI"))
{
    var data = (Intent)intent.Extras;
    ProcessIntent(intent);
}

How do I retrieve Intent in second activity?

This line: intent.PutExtras(intent); will copy all extra data of intent. Assume intent has: "DATA" and "CLIPDATA" keys. You can retrieve like this in your SecondActivity

string text = Intent.GetStringExtra ("DATA") ?? "Data not available";

The Xamarin documentation has a page that gives an example of doing exactly what you're asking, passing data from one activity to another via intent extras.

In summary you'll need to call the following method in the receiving activity:

string text = Intent.GetStringExtra ("FilesURI") ?? "Data not available";

A more detailed example can be found in the Xamarin documentation, where the above sample was extracted from: Passing Data Between Activities

Here's the solution, thanks to @YodaScholtz for the hint.

I wrote the following code

In MainActivity

Intent intent = new Intent(this, typeof(SecondActivity));
intent.PutExtra("ClipData", intent.ClipData);
StartActivity(intent);

SecondActivity code

if (intent.HasExtra("ClipData"))
{
  var _data = intent.Extras;
  var _clipdata = (ClipData)_data?.GetParcelable("ClipData");
  intent.ClipData = _clipdata;
  ProcessIntent(intent);
}

from one activity to others activity

Intent intent = new intent(Mainactivity.this,anotheractivityname.class); startactivity(intent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM