Reputation: 1251
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 ?
Upvotes: 0
Views: 783
Reputation: 4069
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.
Upvotes: 0
Reputation: 4387
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.
Upvotes: 0
Reputation: 303
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)
Upvotes: 1