Zapnologica Zapnologica - 1 year ago 159
Android Question

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

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
and just
(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?


The fact that my phone's back button also closes my entire app, leads me to think that i have started up the second 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 itself.

Answer Source

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


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

On pressing the back key the activity automatically finish.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download