OnCreate flow 在 Finish()之后继续

我想从 onCreate方法内部完成一个活动。当我调用 finish()时,没有立即调用 onDestroy(),代码不断流经 finish()。在 onCreate()闭合花括号之后才调用 onDestroy()

根据 ABc0 developer.android.com/reference 的描述。

在这种情况下,可以从这个函数中调用 Finish () OnDestroy ()将被立即调用,而不使用任何其他 活动生命周期(onStart ()、 onResume ()、 onPuse ()等)执行。

我问这个问题的原因是: 我想检查从 Bundle 传递到 onCreate()的数据。当然,我有什么是传递到 onCreate的控制,但我仍然认为它应该在交付点检查。

我的代码包含启动活动 B的类 A。我认为最后两个“ out of 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

我的代码类别 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-2615:53:40.456: INFO/tag (699) : A Class: goToBButton,onClick

11-2615:53:40.636: INFO/tag (699) : A Class: onPuse

11-2615:53:40.865: INFO/tag (699) : B Class: one line before 1 final

11-2615:53:40.896: INFO/tag (699) : B 类: 在 if 子句之外, 在第二次完成之前

11-2615:53:40.917: INFO/tag (699) : B 类: if 子句之外, 完成后

11-2615:53:41.035: INFO/tag (699) : A Class: onResume

11-2615:53:41.165: INFO/tag (699) : B Class: onDestroy

34519 次浏览

I'm guessing that it is because finish() doesn't cause the onCreate method to return. You could try simply adding

finish();
return;

Or use an if else

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutb);
if(good data){
//do stuff
}else{
finish();
}
}

It seems like finish() does not work until onCreate() return control to system. Please refer to this post: about finish() in android. You have to consider this issue if you don't want any of your code to be executed after calling finish.

Hope it helps.