Note that there are some explanatory texts on larger screens.

plurals
  1. POonCreate flow continues after finish()
    primarykey
    data
    text
    <p>I would like to finish an activity from inside the <code>onCreate</code> method. When I call <code>finish()</code>, <code>onDestroy()</code> is not immediately called, the code keeps flowing past <code>finish()</code>. <code>onDestroy()</code> isn't called until after the <code>onCreate()</code> closing brace.</p> <p>Per the <code>onCreate()</code> description at developer.android.com/reference.</p> <blockquote> <p>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.</p> </blockquote> <p>Reason I ask is: I would like to check data from the Bundle passed to <code>onCreate()</code>. Of course I have control of what is passed to <code>onCreate</code>, but I still think it should be checked at the point of delivery.</p> <p>My code contains class <code>A</code>, which starts Activity <code>B</code>. I believe the last two "outside of if clause" tags, shouldn't be called because the <code>finish</code> method in the <code>if</code> statement should have destroyed the activity. It has nothing to do with the if clause because the tag line after the second <code>finish()</code> call is still also read.</p> <p>My Code:</p> <p>Class A</p> <pre><code>@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 </code></pre> <p>My Code ClassB</p> <pre><code> 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 </code></pre> <p>Here are the results of my tags:</p> <blockquote> <p>11-26 15:53:40.456: INFO/tag(699): A Class: goToBButton, onClick</p> <p>11-26 15:53:40.636: INFO/tag(699): A Class: onPause</p> <p>11-26 15:53:40.865: INFO/tag(699): B Class: one line before 1st finish</p> <p>11-26 15:53:40.896: INFO/tag(699): B Class: outside of if clause, before second finish</p> <p>11-26 15:53:40.917: INFO/tag(699): B Class: outside of if clause, after finish</p> <p>11-26 15:53:41.035: INFO/tag(699): A Class: onResume</p> <p>11-26 15:53:41.165: INFO/tag(699): B Class: onDestroy</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload