Search code examples
androidandroid-activityandroid-lifecycle

Destroying activity when App looses focus


I have an Android application and I faced a weird problem. I have an activity and in the onStop() method I call finish() like so:

@Override
public void onStop() {
    super.onStop();
    finish();
}

The problem is that when I close the activity by pressing the round button in the navigation bar and then reopen the app from the recent apps tab in the navgiation bar then the activity is just recreated. That means the onCreate() method is called. What I would like instead is that the MainActivity is opened.

What am I doing wrong?


Solution

  • You should be able to accomplish what you want by adding

    android:noHistory="true"
    

    to the <activity> declaration in your manifest.

    In that case you don't need to add the all to finish() in onStop().

    I'm assuming your app has only one Activity.