Skip to content Skip to sidebar Skip to footer

Android Activity Singleton

I have an activity called MainActivity. This activity launches a notification that has a PendingIntent that opens this MainActivity. So, to close the application, I have to click t

Solution 1:

singleInstance and singleTask are not recommended for general use.

Try:

 android:launchMode="singleTop"

For more information please refer to launchMode section of the Activity element documentation.

In addition to the previous reference you should also read tasks and back stack

Solution 2:

If you need to return to your app without creating a new instance of your activity, you can use the same intent filters as android uses when launching the app:

finalIntentnotificationIntent=newIntent(context, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0);

As the intent you created to open your activity from the notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.

Post a Comment for "Android Activity Singleton"