Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

android - Resume application and stack from notification

I want to resume my app from a status bar notification in the exact same manner as when the user taps its icon in the launcher.

That is: I want the stack to be in the same state as it was before the user left it.

The problem when setting a pending intent on the notification is that it always targets a specific activity. I don't want this. I need to resume the application just as the launcher does.

So if the user is in activity A, I want to resume activity A. If he has launched activity B from activity A, then I want B to be displayed when the user taps the notification, and the stack to be restored so that A gets resumed when the user taps the back button in B.

There are couple of other questions of questions with similar titles, but none address my problem.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use the same intent filters as Android uses when it launches the app:

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...