开发者

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 se开发者_Python百科t up activity as singleton. I tried to set singleInstance or singleTask to manifest but this doesn't work.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜