Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the back button twice. I would like to set up activity as singleton. I tried to set singleInstance or singleTask to manifest but this doesn't work.

like image 785
CeccoCQ Avatar asked Sep 20 '11 19:09

CeccoCQ


2 Answers

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

like image 64
Moog Avatar answered Sep 17 '22 14:09

Moog


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:

final Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = 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.

like image 35
GrAnd Avatar answered Sep 17 '22 14:09

GrAnd