Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:16:44+00:00 2026-05-27T07:16:44+00:00

I would like to finish an activity from inside the onCreate method. When I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:16:45+00:00Added an answer on May 27, 2026 at 7:16 am

    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();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to call a particular method that's in an Activity from a
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
So I have my Activity, and on pressing a Quit button I call Activity.finish().
I would like to upgrade Subclipse for Eclipse from 1.6 to 1.8. But I
I would like to create a version of Java's deflater not synchronizing on its
I would like to play my video 2 times, When i call the function
I have an activity with a Dialog theme and I would like to close
I would like to start a new activity for a result , with startActvityForResult()
I would like to know if I can freeze the current Activity, while I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.