I have a splash activity (A) that calls a listview activity (B) which calls another activity (C).
When I'm on activity C and I press Home, than kill the app (or wait of Android to do it), than longpress Home and come back to activity C there's a strange problem:
When I click back I go back to B. Than I have a backbutton handler that asks the user if they want to exit and calls finish() on the activity. When I try to exit in this scenario, activity A starts again.
On regular operation it finishes B and doesn't go back to A.
Why is that??
Thanks
When the app is killed (either by you or by Android) the process hosting your applications is killed. However, Android remembers the state of the activity stack (in your case A->B->C).
When the user returns to the app, Android creates a new process for the app and recreates only the activity that was at the top of the activity stack (in this case: C). Now the user presses BACK, which causes activity C to finish and Android recreates the instance of activity B which is then shown (You will see calls to B.onCreate(), B.onStart() and B.onResume()).
Now the user presses BACK again. Your back button handler tries to call finish() on activity A, but there is no instance of activity A. Android hasn't created it yet! When activity B finishes Android remembers that there was an instance of activity A in the activity stack underneath B so it recreates the instance of activity A which is then shown (You will see calls to A.onCreate(), A.onStart() and A.onResume()).
I hope this explains what you are seeing.
Make sure you are calling finish() on A when you load B
Related
I have an application that implements two activities. Activity A is for selecting some files on device and activity B is to show additional info while these files are processing. Both of them have singleInstance as launch mode.
On application's start activity A runs. Then this activity starts second activity B, that creates a notification. If I tap this notification or open running app from recents, it works fine and shows running activity B. But if I'd started application before and activity B is already running, launching it again from app menu causes showing activity A when old activity B is already running and accesible from notification bar.
So, what should I do to make application run only single activity at the same time and show second activity when called from launcher (if second activity once started and isn't finished)?
Your launcher intent should be mapped to a dummy activity, with no view (or may be a splash who knows !). In onCreate of that dummy activity you check if a state was saved previously and based on the state switch to the desired activity.
And how to set the state ? well lets try something simple. lets say when Activity A resumes we put "A" to some key in SharedPreference. if Activity B resumes we put "B" to the same key in preferences. Now your application goes out when Activity B was visible, the state saved would be "B", you launch from home screen, the dummy activity finds "B" in the state and launch intent to go to B else go to A.
Actually the scenario is a bit more complex than described it the title.
The situation is the following:
Activity A starts Activity B.
Activity A must not be destroyed when I start Activity B because I
need the user to be able to navigate back to A.
When the user presses the HOME button the user opens the Recent Apps
window and switches from my app to another app. At this stage both A
and B are STOPPED.
When the user user opens the Recent Apps window and switches back for
the other app to my app: Activity B is RESTARTED (activity A is not
restarted yet)
Now on Activity B there is a button to close the entire app, closing
both B and A, and it does close both activities using this approach:
https://stackoverflow.com/a/11509279/1815311
THE PROBLEM IS THAT WHEN ACTIVITY B TRIES TO CLOSE BOTH B AND A, IN
THE DESCRIBED SCENARIO, ACTIVITY A SOMETIMES IS NULL !!
How do I cope with such a scenario?
1 solution is - before finishing the activity B, store some variable with value 1 using sharedpreferences, finish activity B but not A. system will resume activity A. override onResume() function in activity A and get the variable from shared preferences, if it states 1 then store 0 there, and finish() activity A.
2 solution is overriding onresultactivity - see here
How to kill an application with all its activities?
I have an app that starts with Activity A. Among other things user can go to Activity B and return with the back button.
User can start Activity A also from Activity B using a Menu. This brings Activity A to Front again.
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Problem is: Activity A should be Home Activity. Pressing back needs to always quit the application.
Currently back will go back to Activity B, then finish.
Using FLAG_ACTIVITY_CLEAR_TOP will destroy Activity B, which I do not want as the user should find the activity where he left it (e.g. scrolling position; unless android destroyed it for memory reasons).
I just want to leave the application on back pressed on Activity A. How can I do that?
Can I clear the backstack without destroying the activities?
As an alternative I could override onBackPressed in Activity A to quit the app, but how do I exit the application? Using finish() will only close current activty an bring back other open activities. Do I nees a broadcast as suggested here: Clear Activity back stack?
Imagine an app where Activity A is the default Activity and has launchMode="singleTask". The app opens to Activity A. The user navigates through the app and at some point navigates to Activity A again (by moving forward, not pressing back). According to the Android docs, because launchMode="singleTask", this will happen:
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
Activity A exists on the back stack, so it will receive a call to onNewIntent(). What I want to know is, what happens to the other Activities when Activity A is brought from the bottom of the back stack to the top?
Are they popped and destroyed or does something else happen? Are they even in the same stack?
Thanks in advance...
From the Android docs:
http://developer.android.com/images/fundamentals/diagram_backstack_singletask_multiactivity.png
A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.
I have an application which consists 3 activities - lets call them A, B & C.
From activity A You can get to activity B, from B to C and from C to A (By pressing the back button).
On activity A I have an exit button (I know it`s not recomandded on android, but a lot of users are asking for it, so I decided to add it).
Anyway the problem is that: activity B has a timer, when the timer is finished it starts an intent that starts activiy C and calls finish() on activity B.
When the user then press back on activity C he gets to activty A - and then when he presses the exit button (this button only preforms finish()), it pops up activity B again. How can I prevent it from happen?
Thanks!
Idea: set a isFinished field in your Application to true and call finish(). All activities check if isFinished in their onResume() and if true they finish().
I'm not sure I understand how you end up at A when going back from C. Shouldn't it go back to B (logically, I mean. I know you want it to go to A)? In fact, that seems to kind of be what's happening. B is getting relaunched, but A appears on top of it, so that when you finish A, B (which was under it) is now shown.
It sounds like you don't want B to be part of the history stack, so maybe when you launch B, you should launch it with the no history flag.
You activity b is not finished thats what i can figure out it is still in running state so when all other actitivties are finished it shows up please chekc if it is finished or not