Norman
Norman

Reputation: 33

Xamarin Forms / Android - Intent Extras is null

i am trying to open image intent (select files from gallery) but need to pass certain id (project / report id) which needs to be processed for intent result.

Whatever i have tried for intent options / flags, putting extra for intent (string) is working, but on result extras are empty always. this is the related code (both in mainactivity):

var imageIntent = new Intent(Intent.ActionPick);
                    imageIntent.SetType("image/*");
                    imageIntent.PutExtra(Intent.ExtraAllowMultiple, true);
                    imageIntent.SetAction(Intent.ActionGetContent);
                    imageIntent.AddFlags (ActivityFlags.GrantReadUriPermission);

                    if (ProjectId != null && ProjectId.ToString().Length > 0) imageIntent.PutExtra("ProjectId", ProjectId.ToString());
                    if (ReportId != null && ReportId.ToString().Length > 0) imageIntent.PutExtra("ReportId", ReportId.ToString());

                    CrossCurrentActivity.Current.Activity.StartActivityForResult(imageIntent, requestCode);

and

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

        ...

        if (data.Extras != null)
        {
          if (data.GetStringExtra ("ProjectId") != null) int.TryParse(data.GetStringExtra("ProjectId"), out ProjectId);
          if (data.GetStringExtra("ReportId") != null) Guid.TryParse(data.GetStringExtra("ReportId"), out ReportId);
        }

data.Extras is null here.

Any suggestions?

Thanks in advance..

Upvotes: 0

Views: 1071

Answers (1)

Leon Lu
Leon Lu

Reputation: 9234

data.Extras is null

It is correct, there are two Intents.

When you use StartActivityForResult to open the Gallery, this imageIntent we can called Intent1, Intent1 contains ProjectId and ReportId. When you select the Image, back to the previous activity, System will generate a new Intent, we can called Intent2. Itent2 just contains you select image information, it not contains ProjectId and ReportId, So you get null with data.Extras in OnActivityResult.

If you want to get the ProjectId and ReportId information in any activity or other classes in android project, you can use SharedPreferences to achieve it.

How do I use SharedPreferences in Xamarin.Android?

Upvotes: 1

Related Questions