我想从内部完成一项活动onCreate
方法。当我打电话时finish()
, onDestroy()
不会立即调用,代码不断流过finish()
. onDestroy()
直到之后才被调用onCreate()
右大括号。
Per the onCreate()
描述位于developer.android.com/reference。
您可以从此函数中调用 finish(),在这种情况下
onDestroy() 将立即被调用,而无需执行任何其余操作
活动生命周期(onStart()、onResume()、onPause() 等)正在执行。
我问的原因是:我想检查传递到的捆绑包中的数据onCreate()
。当然,我可以控制传递给的内容onCreate
,但我还是认为应该在交货时检查一下。
我的代码包含类A
,启动 ActivityB
。我相信最后两个“if 子句之外”标签不应该被调用,因为finish
方法中的if
声明应该已经破坏了该活动。它与 if 子句无关,因为第二个之后的标记行finish()
调用仍还读。
My Code:
Class 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
我的代码B类
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:信息/标签(699):A类:goToBButton,onClick
11-26 15:53:40.636:信息/标签(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:信息/标签(699):A 类:onResume
11-26 15:53:41.165:信息/标签(699):B 类:onDestroy