Reuse Activity when launching APP via Intent (Notification)

Hi,

my app is sending notifications to the user, which should open a certain page in the app when tapped.
Passing parameters works fine, opening the page, too.
But one problem drives me mad: every time when the app is called from the notification-intent, a new activity-instance is created and the "old" one continues to exist. But I'd like to reuse the main activity.

Does anybody know how to archive reusing the main activiy when calling the app via intent from a notification?

Regards, Sascha

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    You need to specify the correct launch mode

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Specifically, I think what you need is to use "SingleTop" and then override the OnNewIntent method.

  • Could you give an example how to do this?
    Especially the override of OnNewIntent.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Something like this:

    [Activity(Label = "FormsPlayground.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
    
            LoadApplication(new App());
        }
    
        protected override void OnNewIntent(Intent intent)
        {
            ((App)App.Current).UpdateMainPage();
        }
    }
    

    UpdateMainPage is a new method that you would write (probably with a better name and maybe some arguments derived from the Intent). Note that I added the launch mode as well.

  • FernandoVMFernandoVM BRMember ✭✭

    @adamkemp said:
    Something like this:
    { . . . }
    UpdateMainPage is a new method that you would write (probably with a better name and maybe some arguments derived from the Intent). Note that I added the launch mode as well.

    Dear, I already have tried a lot of ways but nothing working, could you (or somebody) see my sample in https://github.com/fernandovm/xamarin.topics that I coded to show my problem.. All my attempts considering LaunchMode, context, notification manager, activity flags and etc. has failed...

  • ff.5037ff.5037 ILMember

    var intent = new Intent(this, typeof(Activity1)).SetFlags(ActivityFlags.ReorderToFront);
    StartActivity(intent);

Sign In or Register to comment.