今天面试被问及了一个问题:Activity A、Activity B,Activity A 被B覆盖的时候,Activity生命周期中哪几个方法被调用了?Activity A、Dialog B对话框覆盖了Activity A,Activity生命周期中哪些方法被调用了?答案等下揭晓:
我们经常使用Activity,也知道Activity的生命周期中有onCreate、onStart、onResume、onPause、onStop、onDestroy、onRestart这些方法,那么这些方法什么时候会被调用呢?
首先我们来看下面经典的生命周期图流程图:
相信不少朋友看过这个流程图,也基本了解了Activity的生命周期:
1,启动一个Activity: 系统会调用 onCreate()--->onStart()--->onResume(),Activity进入运行状态。
2,用户退出当前Activity并关闭应用(如点击返回按钮):系统会调用 onPause()--->onStop()--->onDestroy(),
3,当前Activity被其他Activity覆盖或点击电源键:系统会调用 onPause()--->onStop();Activity进入停滞状态
4,当前Activity跳转到一个新的Activity界面或者点击Home键:系统会调用 onPause()--->onStop();Activity进入停滞状态
5,从上一个Activity返回到当前Activity:系统会调用 onRestart()--->onStart()--->onResume();当前Activity进入运行状态。
6,横竖屏切换:当前Activity 系统会调用 onPause()--->onStop()--->onDestroy(); 然后重新创建一个新的Activity:onCreate()--->onStart()--->onResume();
那么今天面试中的问题:弹出对话框生命周期会是什么情况呢?
7,弹出对话框(PopupWindow,Dialog):
此时系统并没有将Activity从栈顶移除或是有另一个Activity覆盖当前Activity的情况。所以系统并不会调用Activity中生命周期中的方法。
下面结合实例,来演示一下各个情况:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Button mBtnShowDialog;
private Button mBtnShowPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnShowDialog = (Button) findViewById(R.id.button);
mBtnShowPopWindow = (Button) findViewById(R.id.btn_show_pop);
mBtnShowDialog.setOnClickListener(listener);
mBtnShowPopWindow.setOnClickListener(listener);
Log.i(TAG, "==onCreate()==");
}
// 点击事件
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
doShowDialog();
break;
case R.id.btn_show_pop:
doShowPopWindow(v);
break;
}
}
};
// 显示对话框
public void doShowDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("对话框");
builder.setMessage("对话框消息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
startActivity(intent);
}
});
builder.show();
}
// 显示弹窗
public void doShowPopWindow(View v){
PopupWindow window = new PopupWindow();
View view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
window.setContentView(view);
window.setWidth(100);
window.setHeight(100);
window.showAsDropDown(v);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.i(TAG,"==onWindowFocusChanged==" + hasFocus);
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "==onRestart==");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "==onStart()==");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "==onResume()==");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "==onPause()==");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "==onStop()==");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "==onDestroy()==");
}
// 此方法在被横竖屏切换时会被调用,
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"==onRestoreInstanceState==");
}
// 主要用来在Activity失去焦点,Activity不可显示前保存一些状态值,该方法在onStart方法之后在onStop方法之前被调用
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "==onSaveInstanceState==");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "==onConfigurationChanged==");
}
}
1,onWindowFocusChanged方法:Called when the current Window of the activity gains or loses focus.当窗口获取或者失去焦点的时候会被调用。
(1)在Activity被首次被创建,窗口获取到焦点
(2)点击Home键或者点击键锁屏
(3)再次显示应用
还有当用户退出当前Activity,当前Activity被其他Activity覆盖的时候同样也会被调用。(建议屏蔽该方法,以方便后面的测试)
2,onSaveInstanceState方法:Called to retrieve per-instance state from an activity before being killed so that the state can be restored inonCreate(Bundle) or onRestoreInstanceState(Bundle) 在Activity被系统Kill前保存一些状态值,以便在onCreate或者onRestoreInstanceState方法使用。
3,onRestoreInstanceState方法:This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState.当前Activity重新被初始化的时候调用。
4,onConfigurationChanged方法:Called by the system when the device configuration changes while your component is running.当设备的配置发生改变时会被调用。
测试机型:小米2A
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/jczwilliam312/article/details/47285287