android developer
android developer

Reputation: 116020

android - return to app from service or notification

i'm having some problems with the flow of an app i'm working on. basically , i have a service that always holds a notification , pressing on the notification should return to the app's most recent activity , without re-opening it (meaning that it will resume). also , on a specific activity (and maybe others ) , i need that clicking on the back button would exit the app (and the service) , so the next time the end user starts it via the launcher or via long pressing the home button , it will go back to the first activity .

in short , the requirements are:

so , for example , if i have activity A which calls activity B (which is the special activity) : if the end user has clicked home , and then returned to the app via the notification (or launched via launcher/long press on home button) , it will return to the exact state of activity B that he left it . also , if the end user has pressed the back button on activity B , the app is closed (and the service and notifications shall be gone) the next time he opens the app (no matter how) , he will go back to activity A .

i've tried to use "singleInstance" on activity B , but then it will always get back to activity B , since it is inside its own task , no matter which flags i use(i have tried FLAG_ACTIVITY_REORDER_TO_FRONT and some other flags) . without using it , the notification will open a new instance of activity B .

can anyone please help ?

Upvotes: 0

Views: 1072

Answers (3)

android developer
android developer

Reputation: 116020

jelly bean (android 4.1) now introduces a new API for this exact problem :

http://www.youtube.com/watch?feature=player_embedded&v=Yc8YrVc47TI#t=830s

however , i'm not sure i understand how to use it and how it works. is it possible that it re-creates the entire stack of the activities ? isn't it quite problematic since they might include data that wasn't there before (since they are refreshed) ?

it also sounds problematic since it means that i need to monitor all of the actions in order to restore them back later.

Upvotes: 1

android developer
android developer

Reputation: 116020

an alternative way would be to set the notification's intent to start a new , fake activity , that will close as soon as it is created. the intent will also have the "FLAG_ACTIVITY_NEW_TASK" flag .

hopefully this method will work for everyone . too bad this solution seem more like a workaround than a real solution.

another alternative would be this link: Change notification intent in Android

Upvotes: 1

android developer
android developer

Reputation: 116020

ok , even though it's not exactly the answer , for my case , i've used "singleTop" for activity B , and chose to close activity A when moving to activity B .

Upvotes: 0

Related Questions