24

I don't want my application to show few Activity (say SplashScreenActivity) when pressing back button. So I've used noHistory=true in my Manifest.xml for that Activity as show below:

<activity
    android:name="com.gokul.SplashScreenActivity"
    android:noHistory="true" >
</activity>

Instead of setting noHistory, I can also call finish() in my SplashActivity.onPause() method or wherever I want, as shown below:

@Override
protected void onPause() {
    super.onPause();
    finish();
}

Both does the job perfectly. But which one is better to use, to use noHistory or call finish()?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
  • 4
    Please note that noHistory has also "side effects" - when you put the noHistory activity to background (e.g. press Home button) when user returns to it (via "recent apps"), it will not be shown – Marian Paździoch Aug 14 '14 at 12:12
  • So dear @MarianPaździoch how can we terminate app when user both pressed home and back button? Because my Google TTS, and Locale library glitched when user pressed home button. The manifest code of android:noHistory="true" only fix the issue when user pressed back button, but home button still buggy. Thank you. – Bay Oct 18 '20 at 22:14
  • android:excludeFromRecents="true" this will terminate app when pressed home and back button on the device seems like the app had never opened :D – Bay Oct 18 '20 at 22:24

4 Answers4

27

onPause() is not at all a good place to put a finish() call for your activity. onPause() can be called for a variety of reasons, and I doubt your users would be very pleased to see that whatever they were doing was simply forgotten by your app if they, for instance, turn the screen off and back on.

noHistory should serve you just fine, but you can get a similar behavior by calling finish() in your Activity immediately after it launches a new Activity. However, noHistory is more maintainable in the end, because you may end up forgetting to include the finish() call if you add another startActivity() call to your SplashActivity later.

NasaGeek
  • 2,138
  • 1
  • 15
  • 19
  • 1
    It's worth noting that one of the downsides of using a `noHistory = true` declaration in your manifest is that if your noHistory activity launches a 3rd party intent (camera, google sign in, etc) then when a user presses the back button they can't return to the activity they were just using that launched the 3rd party window, they'll return to their device's home screen. – Cody May 17 '20 at 15:20
8

If you want to call finish(), you should do it after a startActivity call, if you do it in any of the lifecycle callbacks, as NasaGeek says, it could be called in diferent moments, and not only when navigating to other activity.

The good thing of noHistory, is that the system takes care of finising the activity in the correct moment (as you can read here, the system will actually call the finish() method for you) so you can be sure that you are not calling it in a bad moment, and everything is safe (probably this doesn't seem important now, but it could be, for instance when working with threads)

Also, the good thing of it, is that the activity is not only finished when you launch another activity, but also in any other case when the user navigates away from it and the activity no longer visible on screen, for instance when the user press the back button (as you wanted) or when another activity comes to the foreground.

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
  • 1
    I had exactly the behaviour you explain in your last paragraph and it was turning me crazy haha I wanted to see that it is the normal behaviour but i couldn't find it anywhere. Thanks! PS: If you think about it, using noHistory or finish() is not the same as some people say. – Kaizie Mar 31 '17 at 11:14
0

Don't use onPause() as is called when the activity goes in the background. And this case can be anytime such as on intent or popup message(custom). It's better to use another option rather playing with the life cycle of the activity. nohistory option if it works for you then it is nice. You can try following also,

<activity
    android:name="com.gokul.SplashScreenActivity"
    android:launchMode="singleTask"
    android:clearTaskOnLaunch="true"/>
Erik Ghonyan
  • 427
  • 4
  • 12
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
0

When you use noHistory, there could be some weird issues when you ask for permissions on that activity.

On Samsung Galaxy S5(Android 6), the activity is closed when the permissions dialog is displayed

ylinkz
  • 181
  • 1
  • 2
  • 13