locked
Reuse Activity when launching APP via Intent (Notification) RRS feed

  • Question

  • User5742 posted

    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

    Tuesday, December 22, 2015 6:33 PM

All replies

  • User129559 posted

    You need to specify the correct launch mode

    Tuesday, December 22, 2015 7:15 PM
  • User181 posted

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

    Tuesday, December 22, 2015 7:41 PM
  • User5742 posted

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

    Wednesday, December 23, 2015 7:51 AM
  • User181 posted

    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.

    Wednesday, December 23, 2015 5:02 PM
  • User97727 posted

    @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...

    Monday, February 20, 2017 11:17 PM
  • User204959 posted

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

    Monday, September 3, 2018 8:20 PM
  • User390081 posted

    @SaschaSchwegelbauer said: 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

    Have you solved the problem yet??? I am having the same problem. Can you help me???

    Thursday, October 31, 2019 4:17 AM