的onCreate流继续完成之后()继续、onCreate

由网友(这日子过得像句废话°)分享简介:我想从的onCreate 方法内完成的活动。当我打电话完成(),的onDestroy()不会立即调用,code保持流经完成()。 的onDestroy()不叫后才的onCreate()右括号。每的onCreate()的说明在developer.android.com/reference。您可以从这个函数中调用finis...

我想从的onCreate 方法内完成的活动。当我打电话完成()的onDestroy()不会立即调用,code保持流经完成()的onDestroy()不叫后才的onCreate()右括号。

的onCreate()的说明在developer.android.com/reference。

  

您可以从这个函数中调用finish(),在这种情况下,   的onDestroy()将被立即调用没有任何的休息的   活动周期(ONSTART(),onResume(),在onPause()等)执行。

原因我想问的是:我想检查数据从包传递到的onCreate()。当然,我有什么传递给的onCreate 控制,但我仍然认为它应在交货点进行检查。

我的code包含类 A ,这将启动活动 B 。我相信标签,不应该叫过去两年,如果条款之外,因为完成的方法如果语句应该已经销毁活动。这是因为在标记线的第二个完成()呼叫仍然还阅读无关的if语句。

我的code:

A类

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    // goToBButton:当pressed发送消息给B类
    按钮goToBButton =(按钮)this.findViewById(R.id.go_to__b_btn);
    goToBButton.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            Log.i(TAG,A类:goToBButton,的onClick);
            意图I =新的意图(A.this,B.class);
            startActivityForResult(ⅰ,REQ_TO_B);
        }
    });
} //结束的onCreate
 

我的$ C C ClassB的$

 公共类B扩展活动{

私有静态最后字符串变量=标签;

@覆盖
   公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.layoutb);

  //设置为真,要经常打印标签:第一次之前完成一条线
  如果(真){

    Log.i(TAG,B类:一行1日之前完成);
    完();
  }

  //不是应该先完成之后来到这里
  Log.i(TAG,B类:if语句之外,之前第二整理);
  完();
  //不应该第二整理后到达这里
  Log.i(TAG,B类:if语句之外,之后完成);
   } //结束的onCreate


@覆盖
公共无效的OnStart(){
    super.onStart();
    Log.i(TAG,B类:ONSTART);
}

@覆盖
公共无效onRestart(){
    super.onRestart();
    Log.i(TAG,B类:onRestart);
}

@覆盖
公共无效onResume(){
    super.onResume();
    Log.i(TAG,B类:onResume);
}

@覆盖
公共无效的onPause(){
    super.onPause();
    Log.i(TAG,B类:在onPause);
}

@覆盖
公共无效的onStop(){
    super.onStop();
    Log.i(TAG,B类:的onStop);
}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    Log.i(TAG,B类:的onDestroy);
}

 } //结束B类
 
知情人爆料 369夏季赛结束后转会 不可能在TES继续打下去了

下面是我的标签结果:

  

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:信息/标签(699):B级:1完成前一行

     

11-26 15:53:40.896:信息/标签(699):B类:if语句之外,   第二整理之前

     

11-26 15:53:40.917:信息/标签(699):B类:if语句之外,   说完之后

     

11-26 15:53:41.035:信息/标签(699):A类:onResume

     

11-26 15:53:41.165:信息/标签(699):B类:的onDestroy

解决方案

我猜测,这是因为完成()不会导致onCreate方法返回。你可以尝试简单地增加

 结束();
返回;
 

或使用的if else

  @覆盖
公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.layoutb);
  如果(良好的数据){
      //做的东西
  }其他{
      完();
  }
}
 

I would like to finish an activity from inside the onCreate method. When I call finish(), onDestroy() is not immediately called, the code keeps flowing past finish(). onDestroy() isn't called until after the onCreate() closing brace.

Per the onCreate() description at developer.android.com/reference.

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.

Reason I ask is: I would like to check data from the Bundle passed to onCreate(). Of course I have control of what is passed to onCreate, but I still think it should be checked at the point of delivery.

My code contains class A, which starts Activity B. I believe the last two "outside of if clause" tags, shouldn't be called because the finish method in the if statement should have destroyed the activity. It has nothing to do with the if clause because the tag line after the second finish() call is still also read.

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

My Code 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

Here are the results of my tags:

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

解决方案

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();
  }
}

阅读全文

相关推荐

最新文章