简体   繁体   中英

Not Able To Get Intent Extra Text In Second Time When App In Background Xamarin.Android

I using This code to start my activity with Intent Extra Text and at first time it work fine but when i again sharing a text with my app then it just launch my app but not extra text showing at all. can anybody help me

Note: doing some modification in coding it showing same intent extra text again again.

Here is MainActiviy Code

     protected override void OnCreate(Bundle bundle)        {

     Intent intent = Intent;
        string action = intent.Action;          
        string type = intent.Type;           
        if (Intent.ActionSend.Equals(action) && type != null)
        {
           // intent.AddFlags(ActivityFlags.NewTask);
            if ("text/plain".Equals(type))
            {
                handleSendText(intent); // Handle text being sent
            }
        }
          }

     public void handleSendText(Intent intent)
    {         
        string sharedText = intent.GetStringExtra(Intent.ExtraText);            
        if (sharedText != null)
        {              
          Toast.MakeText(this, sharedText, ToastLength.Long).Show();
        }
    }

And AndroidManifest.Xml

     <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/*"/>
        </intent-filter>

Everything Working Fine But The Problem Is At Second Time When App In Background And I Go To Other App And Sharing Any Text With My App It Just Open My MainActiviy Without New Intent Extra Text Or Same Intent Extra Text. Please Help Me to Sort This Out.

It's because OnCreate() is only called the first time the activity is loaded. You should move your Intent detection code to OnResume() as that's called every time the activity comes to the foreground.

Here's a simplified working example:

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.main_layout);
    var btn = FindViewById<Button>(Resource.Id.btnLoadSenderActivity);
    btn.Click += (sender, e) => { StartActivity(typeof(SenderActivity)); };
}

protected override void OnResume() {
    base.OnResume();
    Intent intent = Intent;
    string sharedText = intent.GetStringExtra("KEY");
    if (sharedText != null) Toast.MakeText(this, sharedText, ToastLength.Short).Show();
}

SenderActivity.cs

protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.sender_layout);
    var btn = FindViewById<Button>(Resource.Id.btnSendIntent);
    btn.Click += (sender, e) => {
        var value = $"Current Date/Time is {DateTime.Now}";
        Intent i = new Intent(this, typeof(MainActivity));
        i.PutExtra("KEY", value);
        StartActivity(i);
    };

}

Hope this helps!

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