您的位置:首页 > 移动开发 > Android开发

[Android基础]Activity的生命周期

2015-08-05 01:09 603 查看
今天面试被问及了一个问题: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==");
}
}


参考其他资料:我添加了onWindowFocusChanged、onSaveInstanceState、onRestoreInstanceState、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重新被初始化的时候调用。

该方法我在测试横竖屏切换的时候会被调用,在点击Home键或者锁屏系统都没有调用onRestoreInstanceState方法。那么什么时候被调用呢?

onRestoreInstanceState()被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity A的onRestoreInstanceState方法不会被执行 此也说明上二者,大多数情况下不成对被使用。

onRestoreInstanceState()在onStart() 和 onPostCreate(Bundle)之间调用。

4,onConfigurationChanged方法:Called by the system when the device configuration changes while your component is running.

当设备的配置发生改变时会被调用。设置Activity的android:configChanges="orientation"时,切屏不会重新调用各个生命周期,切横、竖屏时只会执行一次;

设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法;

但是,自从Android 3.2(API 13),在设置Activity的android:configChanges="orientation|keyboardHidden"后,还是一样会重新调用各个生命周期的。因为screen size也开始跟着设备的横竖切换而改变。所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetSdkVersion属性大于等于13的情况下,如果你想阻止程序在运行时重新加载Activity,除了设置"orientation",你还必须设置"ScreenSize"。

解决方法:

AndroidManifest.xml中设置android:configChanges="orientation|screenSize“

测试机型:小米2A

android SDK:5.1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: