onCreate flow continues after finish()

2019-01-12 01:32发布

问题:

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

回答1:

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


回答2:

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.