Android go back to previous Activity

android, android-activity, android-intent

Solution

you can just call `onBackPressed()` instead of use `Intent` to go back to `MainActivity`..

For example:

public void onClick() {
    onBackPressed();
}

Note: `finish()` should do exactly what you want..

Problem

I have two activities, `MainActivity` and `NextActivity`. From `MainActivity` I can go to `NextActivity` and then use `Intent` to go back to `MainActivity`. But then `OnCreate` and other stuff will be called and it will be like everything is initialized again. I want to go back to the state just like it was before i entered the `NextActivity`. I realized that if I use the physical back button on my phone exactly this is achieved. So how to go back to an activity just like the back button? PS. I tested `finish()` but didn't help.

Original source

Related problems