gpt4 book ai didi

android - onCreate 流程在完成()之后继续

转载 作者:IT老高 更新时间:2023-10-28 13:12:07 29 4
gpt4 key购买 nike

我想在 onCreate 方法中完成一项 Activity 。当我调用 finish() 时,onDestroy() 不会立即被调用,代码会一直流过 finish()onDestroy() 直到 onCreate() 右括号之后才被调用。

根据 developer.android.com/reference 上的 onCreate() 描述。

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

我问的原因是:我想检查传递给 onCreate() 的 Bundle 中的数据。当然,我可以控制传递给 onCreate 的内容,但我仍然认为应该在交付时对其进行检查。

我的代码包含类A,它启动Activity B。我相信最后两个“if 子句之外”标签不应该被调用,因为 if 语句中的 finish 方法应该已经破坏了 Activity 。它与 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-26 15:53:40.456: INFO/tag(699): A Class: goToBButton, onClick

11-26 15:53:40.636: INFO/tag(699): A Class: onPause

11-26 15:53:40.865: INFO/tag(699): B Class: one line before 1st finish

11-26 15:53:40.896: INFO/tag(699): B Class: outside of if clause, before second finish

11-26 15:53:40.917: INFO/tag(699): B Class: outside of if clause, after finish

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

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

最佳答案

我猜这是因为 finish() 不会导致 onCreate 方法返回。您可以尝试简单地添加

finish();
return;

或者使用 if else

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

关于android - onCreate 流程在完成()之后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282569/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com