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
284 views
in Technique[技术] by (71.8m points)

android - How to open last activity from notification status bar?

I want to open last started activity by tapping on the notification in status bar. Suppose I start an Activity A (main activity of my app), this activity sends a notification to notification status bar. activity A also opens an activity B and B opens another activity C. From C i press home button. Now i want to go again to my app so from notification bar i tap on notification (which was sent by A). Here the notification should start activity C because it was last opened.

I did search on this but didn't find proper answer. Thanks in advance.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Few days back I got very very simple solution for my problem. Instead of iterating through recentTasks and getting our task and then getting baseIntent through it, we can do simple thing as follows:

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);

baseIntent contains the same parameters as above Intent has. Hence instead of grabbing baseIntent from recentTasks, it's quite good to use above code.

This notificationIntent will then be passed to pendingIntent for further use.

Provided: MainActivity is the very first activity when we launch our app and in AndroidManifest.xml it must contain IntentFilters of CATEGORY_LAUNCHER and ACTION_MAIN.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...