How to close activity and go back to previous activity in android

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

The above code was run from the main activity.

Now in my new activity which is called by the main activity, I have a back button. When I click on this back button I want my new activity to close and it must go back to the original main activity.

I have tried calling super.finish() and just finish() (from the new activity) but this then closes my entire application (including my main activity).

How can I just close the activity that is currently in focus, and then return to the main activity?

EDITED:

The fact that my phones back button also closes my entire app, leads me to think that i have started up the secondary activity incorrectly?

Ok I have been looking,

I created a Settings Activity that uses the same manifest code and the same code to Start the activity.

For the settings Activity when I push the back button, it returns to the Main activity.

With the activity mentioned above in the main Question it simply exits my entire app. So the problem doesn’t seem to be with the code to finish the activity but the activity its self.

Android: Go back to previous activity

I want to do something simple on android app. How is it possible to go back to a previous activity. What code do I need to go back to previous activity

Android: go back to previous activity? [duplicate]

This question already has an answer here: Android: Go back to previous activity 12 answers Is there a way I can force the user to go back to the previous Activity? I’d like to kill my current A

How to go back to a previous activity in android?

I want to do something simple in an android app. How to go back to a previous activity? I have tried pressing the back button on a smartphone, but it returns to display the application menu and exits

Android go back to previous Activity [duplicate]

This question already has an answer here: Android: Go back to previous activity 12 answers I have two activities, MainActivity and NextActivity. From MainActivity I can go to NextActivity and t

back to previous activity

I try to go to previous Activity (Activity A) but have problem. Command to this get inside Activity B and I wont to go back to B: A: if(…) { B.staticF(); } B: static void staticF() { super.onBackPr

How to go back to the previous activity?

I am developing an application with 5 interfaces I have an activity where the user has to enter a password, after entering it the user moves to the next activity, but when the user clicks on back butt

how can we block previous activity

how can we block previous activity if one go from ActivityA to ActivityB then he want come back previous activity means ActivityA so he should not able to come back in android

Go back to previous activity after a phone call

I am using PhoneStateListener to listen for the changes of the mobile call state. What I am working on is to make a phone call after few seconds and then go back to previous activity. However, each ti

Nexus 5 onCreate() method calls when go back to previous activity

i have a problem about activity lifecycle in nexus 5. When i go back to previous activity onCreate() method calls on previous activity. Normally it should call onStart() method when go back to previou

How to go back to 3rd previous Activity in android?

In my android application I search for a cafes first. When user select one cafe next activity will show more details about it. Then user can view the map of the cafe. When user click on the Done butto

Answers

Just don’t call finish() on your MainActivity then this eliminates the need to Override your onBackPressed() in your SecondActivity unless you are doing other things in that function. If you feel the “need” for this back button then you can simply call finish() on the SecondActivity and that will take you to your MainActivity as long as you haven’t called finish() on it

I think you call the finish() in MainActivity when you start the your SettingsActivity

OR

Mistakenly either you set the android:noHistory = “true” in MainActivity in AndroidManifest.xml.

On pressing the back key the activity automatically finish.

OR

Before you switch to your ‘SettingsActivity’, you have called finish() in your MainActivity, which causes pressing button go back to main screen since MainActivity was already killed.

Button edit = (Button) view.findViewById(R.id.yourButton);
edit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, YourMainActivity.class);
        startActivity(intent);
        finish();
    }
});

I did a lot of things with activities and intents, but don’t remember very well.

Have you tried this?

this.finish();

You are making this too hard. If I understand what you are trying to do correctly, the built-in ‘back’ button and Android itself will do all the work for you: http://developer.android.com/guide/components/tasks-and-back-stack.html

Also, implementing a custom “back” button violates Core App Quality Guideline UX-N1: http://developer.android.com/distribute/googleplay/quality/core.html

When you click your button you can have it call:

super.onBackPressed();

You can go back to the previous activity by just calling finish() in the activity you are on. Note any code after the finish() call will be run – you can just do a return after calling finish() to fix this.

If you want to return results to activity one then when starting activity two you need:

startActivityForResults(myIntent, MY_REQUEST_CODE);

Inside your called activity you can then get the Intent from the onCreate() parameter or used

getIntent();

To set return a result to activity one then in activity two do

setResult(Activity.RESULT_OK, MyIntentToReturn);

If you have no intent to return then just say

setResult(Activity.RESULT_OK);

If the the activity has bad results you can use Activity.RESULT_CANCELED (this is used by default). Then in activity one you do

onActivityResult(int requestCode, int resultCode, Intent data) {
    // Handle the logic for the requestCode, resultCode and data returned...
}

To finish activity two use the same methods with finish() as described above with your results already set.

I don’t know if this is even usefull or not but I was strugling with the same problem and I found a pretty easy way, with only a global boolean variable and onResume() action. In my case, my Activity C if clicked in a specific button it should trigger the finish() of Activity B!

Activity_A -> Activity_B -> Activity_C

Activity_A (opens normally Activity_B)

Activity_B (on some button click opens Activity_C):

// Global:
boolean its_detail = false;
// -------
SharedPreferences prefs =  getApplicationContext().getSharedPreferences("sharedpreferences", 0);
boolean v = prefs.getBoolean("select_client", false);

its_detail = v;

startActivity(C);

@Override
public void onResume(){
     super.onResume();
     if(its_detail == true){
        finish();
     }
}

So, whenever I click the button on Activity C it would do the “onResume()” function of Activity B and go back to Activity A.

try this code instead of finish:

onBackPressed();

if you use fragment u should use

getActivity().onBackPressed();

if you use single activity u can use

finish();
{ getApplicationContext.finish();   }

Try this method..

it may be possible you are calling finish(); in the click button event so the main activity is closed just after you clicking the button and when you are coming back from next activity the application is exit because main activity is already closed and there is no active activity.

I believe your second activity is probably not linked to your main activity as a child activity. Check your AndroidManifest.xml file and see if the <activity> entry for your child activity includes a android:parentActivityName attribute. It should look something like this:

<?xml ...?>
...
<activity
    android:name=".MainActivity"
    ...>
</activity>
<activity
    android:name=".ChildActivity"
    android:parentActivityName=".MainActivity"
    ...>
</activity>
...