I have an Android activity we’ll call A which has a button and another activity B. When the user clicks the button in Activity A, I’d like to finish A (let both onStop and onDestroy finish running) and then start up the instance of B. When I put a finish() and startActivity() call in the button click listener, the instance of B starts up before the old instance of A finishes. Can someone help me figure out a way to do what I’m looking for?
I have an Android activity we’ll call A which has a button and another
Share
What you are looking for is not possible and actually is against Android’s activity lifecycle implementation.
Correction
It is possible with
android:noHistory="true"tag in your manifest, but for what you are trying to do it seems wrong (read the EDIT)… Messing with the activity stack makes a non intuitive application!Android OS doesn’t let you control when activities will be removed from memory (or killed), and therefore all these fancy "Task killers" are so popular (DONT use them, they only make things worse).
When your activity’s
onStop()is being called, the activity stops completely, and it just hangs in your memory, but that’s fine…If you want to reset the state of
activity A, or close the app when exitingactivity B, just create a set of rules in bothonResume()andonStop(), you can do everything you wish by creating a set of rules in those functions.for example: have a boolean in
activity Athat turnstruejust before callingactivity B,call finish() on youractivity A‘s if this boolean istrueI suggest that you take a look at Android’s Activity lifecycle diagram, and make sure that everything you do follows the best practice.
EDIT
I saw your comment, it seems like you are trying to create things that are already in your memory, don’t recreate them, it’s a waste of CPU time, memory, and battery.
Instead, create a static class with a singleton that will hold all your shared data !