Reputation: 12206
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?
Upvotes: 0
Views: 1222
Reputation: 12444
finish()
method.cleanUp()
method.boolean isClean=false
in the activitycleanUp()
write your clean up code.cleanUp()
in your finish()
isCleaned
in finish()
or in cleanUp()
if its true then ignore the cleanB
, call cleanUp()
and set isCleand=true
B
, call finish()
Upvotes: 0
Reputation: 36302
onPause
is a better place to do this cleanup. See the Saving Persistent State section of the Activity doc.
When an activity's onPause() method is called, it should commit to the backing content provider or file any changes the user has made. This ensures that those changes will be seen by any other activity that is about to run. You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.
While I'm not definite that your cleanup is for user changes, the bold sentence above implies that onPause
will complete before the next Activity
is created. Of course that probably implies that you'll have to move some setup to onResume
...
Alternatively, you could move all your cleanup code to a method, let's just call it cleanup
and then just call it before you start activity B. You'll have to put in appropriate guards for your onDestroy
cleanup too of course.
Upvotes: 0
Reputation: 7971
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 exiting activity B
, just create a set of rules in both onResume()
and onStop()
, you can do everything you wish by creating a set of rules in those functions.
for example: have a boolean in activity A
that turns true
just before calling activity B
,call finish() on your activity A
's if this boolean is true
I 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 !
Upvotes: 2
Reputation: 1244
I believe you're looking for onPause()
which is what gets called when the activity is sent to the background. You can do whatever cleanup you want in there. onStop should only be called when a user is exiting out of your program (or launching another one)
Upvotes: 0
Reputation: 11766
Upvotes: -1