我想在
根据developer.android.com/reference中
您可以在此函数内部调用
我之所以问是因为:我想检查传递给
我的代码包含类A,它启动Activity B。我认为最后两个“if语句之外”的标记不应该被调用,因为
我的代码:
类A
onCreate
方法内完成一个活动。当我调用finish()
时,onDestroy()
不会立即被调用,代码会继续执行finish()
之后的内容。只有在onCreate()
闭合括号后,onDestroy()
才会被调用。根据developer.android.com/reference中
onCreate()
的描述:您可以在此函数内部调用
finish()
,这样onDestroy()
将立即被调用,而不执行任何其他活动生命周期(例如onStart()、onResume()、onPause()等)。我之所以问是因为:我想检查传递给
onCreate()
的Bundle中的数据。当然,我控制着传递给onCreate
的内容,但我仍然认为应该在交付时进行检查。我的代码包含类A,它启动Activity B。我认为最后两个“if语句之外”的标记不应该被调用,因为
if
语句中的finish()
方法应该已经销毁了该活动。这与if语句无关,因为第二个finish()
调用之后的标记行仍然被读取。我的代码:
类A
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// goToBButton: when pressed sends message to class B.
Button goToBButton = (Button)this.findViewById(R.id.go_to__b_btn);
goToBButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick (View v) {
Log.i(TAG,"A Class: goToBButton, onClick");
Intent i = new Intent(A.this, B.class);
startActivityForResult(i,REQ_TO_B);
}
});
} // end onCreate
我的代码ClassB
public class B extends Activity{
private static final String TAG = "tag";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutb);
// set as true, should always print Tag: one line before first finish"
if (true) {
Log.i(TAG,"B Class: one line before 1st finish");
finish();
}
// shouldn't get here after first finish
Log.i(TAG,"B Class: outside of if clause, before second finish");
finish();
// shouldn't get here after second finish
Log.i(TAG,"B Class: outside of if clause, after finish");
} // end onCreate
@Override
public void onStart () {
super.onStart();
Log.i(TAG,"B Class: onStart");
}
@Override
public void onRestart() {
super.onRestart();
Log.i(TAG,"B Class: onRestart");
}
@Override
public void onResume () {
super.onResume();
Log.i(TAG,"B Class: onResume");
}
@Override
public void onPause () {
super.onPause();
Log.i(TAG,"B Class: onPause");
}
@Override
public void onStop () {
super.onStop();
Log.i(TAG,"B Class: onStop");
}
@Override
public void onDestroy () {
super.onDestroy();
Log.i(TAG,"B Class: onDestroy");
}
} // end B Class
以下是我的标签结果:
11-26 15:53:40.456: INFO/tag(699): A类:goToBButton,onClick
11-26 15:53:40.636: INFO/tag(699): A类:onPause
11-26 15:53:40.865: INFO/tag(699): B类:第一次结束前一行
11-26 15:53:40.896: INFO/tag(699): B类:在if语句之外,在第二次结束之前
11-26 15:53:40.917: INFO/tag(699): B类:在if语句之外,在结束后
11-26 15:53:41.035: INFO/tag(699): A类:onResume
11-26 15:53:41.165: INFO/tag(699): B类:onDestroy