当我调用startActivity()方法来启动onCreate()方法的其他活动时;
是否执行了其他lifecircle方法,例如onStart()或onResume()
我有一个测试
AppMain.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_main);
Log.i(TAG, "onCreate");
startActivity(new Intent(AppMain.this,AppOther.class));
}
@Override
protected void onRestart() {
Log.i(TAG, "onRestart");
super.onRestart();
}
@Override
protected void onStart() {
Log.i(TAG, "onStart");
super.onStart();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
AppOther.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_other);
Log.i(TAG, "onCreate");
}
@Override
protected void onRestart() {
Log.i(TAG, "onRestart");
super.onRestart();
}
@Override
protected void onStart() {
Log.i(TAG, "onStart");
super.onStart();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
logcat的:
05-29 05:28:11.583: I/AppMain(1257): onCreate
05-29 05:28:11.614: I/AppMain(1257): onStart
05-29 05:28:11.614: I/AppMain(1257): onResume
05-29 05:28:11.643: I/AppMain(1257): onSaveInstanceState
05-29 05:28:11.643: I/AppMain(1257): onPause
05-29 05:28:11.793: I/AppOther(1257): onCreate
05-29 05:28:11.793: I/AppOther(1257): onStart
05-29 05:28:11.793: I/AppOther(1257): onResume
05-29 05:28:12.383: I/AppMain(1257): onStop
我不知道为什么onStart()和onResume()方法仍然可以执行; 似乎startActivity()没有打破AppMain的生命周期
答案 0 :(得分:2)
语言苍白,用代码说话。 试试吧。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(this.getClass().toString(),"onCreate");
//start other Activity
this.startActivity(new Intent(this,OtherActivity.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(this.getClass().toString(),"onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.v(this.getClass().toString(),"onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.v(this.getClass().toString(),"onResume");
}
@Override
protected void onStart() {
super.onStart();
Log.v(this.getClass().toString(),"onStart");
}
}
logcat显示:
答案 1 :(得分:0)
问题的方式太窄但如果你开始另一项活动,当前活动将切换onPause()。如果您完成了被调用的活动,则前一个活动将切换onResume()。简而言之,如果你开始另一个活动,它将不会调用当前活动的onResume()和onStart()。