Activity生命周期:
初次接触,看完周期图,大概了解流程,可是唯一模糊的就是如何判断或者什么操作场景对应某一个周期状态呢?
理论:
1、查看API一目了然:
public class Activity extends ApplicationContext {
protected voidonCreate(Bundle savedInstanceState);
protected voidonStart();
protected voidonRestart();
protected voidonResume();
protected voidonPause();
protected voidonStop();
protected voidonDestroy();
}
关键区分这几个:
onResume():Called when the activity will start interacting with theuser. At this point your activity is at the top of the activity stack, withuser input going to it.
(调用时刻:即当activity将与用户交互,该活动处于活动栈顶部,且拥有用户的焦点)
onPause():当Activity被遮挡住的时候就会调用onPause方法,注意与onStop()区分
onStop():Calledwhen the activity is no longer visible to the user, because another activityhas been resumed and is covering this one. This may happen either because a newactivity is being started, an existing one is being brought in front of thisone, or this one is being destroyed.
(调用时刻:该活动不再可见,且另一个拥有焦点的活动覆盖它。)
2、实践敲代码:(学习老师跟网上的人同样的测试方法,体会activity生命流程)
public class MainActivity extends Activity {
private static final String TAG="ActivityTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "onCreate()");
}
protected void onStart(){
super.onStart();
Log.e(TAG, "onStart(");
}
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
}
了解Activity 的声明周期有何用呢?
我们可以自己定义当处于什么状态的时候做什么事件