Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I start my android-activity first onCreate() is called, then onResume() .

When I then press the back-button to return to the home-screen and tap the app-icon again, first onCreate() is called, then onResume() and then onDestroy() .

My app is still active then, but doing some action result in error since onDestroy() sets a few members to null.

Why is that?

Update: When I wait 30 seconds after pressing back everything works fine. I'm not doing anything heavy in onDestroy except setting a few variables to null and .interrupt() ing a background-thread.

When you press back button activity is popped form the back stack and destroyed. So onDestroy() is called in this case. When you again open the app it should be onCreate() and onResume(). If you want to save or commit something to database do it in onPasue() Raghunandan May 25, 2013 at 11:11 It's fine to accept your own answer when it identifies the key point and remains the best which has been provided. Chris Stratton Jun 12, 2013 at 13:53

onDestroy gets called because, by default, pressing back key results in your activity calling finish() which initiates the destroying of the activity which calls onDestroy().

To prevent doing some action in case the activity is being destroyed do like this:

if(!isFinishing()) {
   // do your action here

isFinishing is a method of the Activity.

I'm not doing anything heavy, just setting a few variables to null. Even when I wait after pressing back, the onDestroy log-message isn't put out. I noticed that when I look at the list of applications after pressing back, it's still there. – Fabian Zeindl May 25, 2013 at 12:00

I think there is something in addition to what you are describing. Android doesn't just keep activity from being destroyed, something MUST be happening on the main thread.

The symptoms sound exactly as if you had either:

  • a service doing a longish HTTP or database operation. Are you sure there are no suxg things?
  • another thread (perhaps managed by an AsyncTask?) calling a synchronized method
  • Same thing is happening with me too. If I wait for few seconds then onDestroy() called, or it will get called immediately when I go to some other screen. I am also surprised and this is happening very first time. I am also not doing any heavy task or any HTTP call. – Smeet Oct 9, 2019 at 12:13

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.