ajma
ajma

Reputation: 12206

Want to start a new activity but let the original activity finish first

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

Answers (5)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

  • override finish() method.
  • implement cleanUp() method.
  • create boolean isClean=false in the activity
  • in cleanUp() write your clean up code.
  • call cleanUp() in your finish()
  • check for isCleaned in finish() or in cleanUp() if its true then ignore the clean
  • now before you start B , call cleanUp() and set isCleand=true
  • after you call B , call finish()

Upvotes: 0

kabuko
kabuko

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

Rotemmiz
Rotemmiz

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

Benoir
Benoir

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

a fair player
a fair player

Reputation: 11766

  1. Start activity A
  2. from inside A startService(c) and finsh A
  3. from inside the service , start Activity B

Upvotes: -1

Related Questions