0

I have a problem with the lifecycle of my android activity. When I press the button "home", the function onDestroy() is calling. I have test with a simple hello world and Toast on callback function.

My code :

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_SHORT).show();
}

    @Override
    protected void onRestart(){
        super.onRestart();
Toast.makeText(getApplicationContext(), "onRestart", Toast.LENGTH_SHORT).show();
    }

    @Override 
    protected void onStart(){
        super.onStart();
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_SHORT).show();
    }

    @Override 
    protected void onResume(){
        super.onResume();
Toast.makeText(getApplicationContext(), "onResume", Toast.LENGTH_SHORT).show();
    }
    @Override 
    protected void onStop(){
        super.onStop();
        Toast.makeText(getApplicationContext(), "onStop", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onDestroy(){
    super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_SHORT).show();
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

EDIT : When I run my application, I have the toast "onCreate", "onState" then "onResume. If I press "home" buton I see the toast "onStop". If I return on my application I have the toast "onStart" and "onResume". But when I run my application on my real device (Samsung GT-P3110), I have the toast "onCreate", "onState" then "onResume. If I press "home" buton I see the toast "onStop" and "onDestroy". (My application is alway visible on the list of running application) If I return on the application, it see "onCreate" -> onState" -> "onResume" like it completly restart.

I don't understand. Can you enlighten me please ?

helene
  • 1,201
  • 2
  • 18
  • 30

3 Answers3

1

An activity represents the screen you see on your phone when app is running, if this screen is gone then your activity is Destroyed.

see this tutorial for info:

http://developer.android.com/training/basics/activity-lifecycle/starting.html

you can read more about this and run the activity apk sample, it will help you alot understanding how it works. (click on Download The Demo and run it somewhere, in a VM or on your phone)

amirelouti
  • 303
  • 1
  • 4
  • 13
  • Thank you for your answer. I understood onDestroyed when the activity is Destroy. But my problem is this case : "In some cases, the activity will move only part way down the pyramid and wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off." In my case, if I switch to another appli, my Application call "onDestroy". I have checked on virtual device, I have the good behavior. The problem is just for my real device (samsung galaxy tab GT-P3110). – helene Oct 22 '13 at 07:27
  • this might be due to differences between versions of your AVD and the real thing, or configurations, as I know myself, ondestroy must be called anyway, maybe samsung changed something in their software, anyway, if you want to restore your app as you left it last time, you can implement onSaveInstanceState() to save your data and onRestoreInstanceState() to restore them when your app is created again. – amirelouti Oct 22 '13 at 11:51
  • In fact my problem was on parameter. The checkbox "delete each activity when user quit" was checked (on development options) – helene Oct 24 '13 at 09:53
0

Your activity is actually destroyed when it is stopped, an excerpt from here:

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one:

the 'except one' in this instance does not apply to you, and it is only when finish() is called from the onCreate() method. The rest of the excerpt is on the bottom of the page linked above.

Samuel
  • 4,337
  • 3
  • 29
  • 35
0

In addition of the comments below, you can make the distinction between the BACK and the HOME button with the method :

void onSaveInstanceState(Bundle outState)

called when you press the HOME button but not the BACK button.

Andros
  • 4,069
  • 1
  • 22
  • 30