Callback | Description |
---|---|
onCreate() | This is the first callback and called when the activity is first created. |
onStart() | This callback is called when the activity becomes visible to the user. |
onResume() | This is called when the user starts interacting with the application. |
onPause() | The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed. |
onStop() | This callback is called when the activity is no longer visible. |
onDestroy() | This callback is called before the activity is destroyed by the system. |
onRestart() | This callback is called when the activity restarts after stopping it. |
关键代码如下:
public class MainActivity extends Activity {
private String msg="Android: ";
/**
* Called when the activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
/**
* Called when the activity is about to become visible.
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(msg, "The onStart() event");
}
/**
* Called when the activity has become visible.
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(msg, "The onResume() event");
}
/**
* Called when another activity is taking focus.
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(msg, "The onPause() event");
}
/**
* Called when the activity is no longer visible.
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(msg, "The onStop() event");
}
/**
* Called just before the activity is destroyed.
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}
运行截图:
当Activity第一次打开时:
当启动另一个Activity时:
当关闭当前的Activity,返回到先前的Activity时:
当销毁当前Activity时: