Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreate flow continues after finish()

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

like image 649
flobacca Avatar asked Nov 26 '11 23:11

flobacca


People also ask

What will happend when you call finish () in onCreate?

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

What finish () function does?

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like: Dismiss any dialogs the activity was managing. Close any cursors the activity was managing.

How many times does onCreate run?

You might want to read through the documentation on the Activity lifecycle. OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

What is used of onCreate () method?

onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.


Video Answer


2 Answers

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();
  }
}
like image 93
triggs Avatar answered Oct 13 '22 11:10

triggs


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.

like image 20
Hicham Avatar answered Oct 13 '22 11:10

Hicham