noHistory vs finish() - Which is preferred?

Gokul Nath KP picture Gokul Nath KP · Jan 27, 2014 · Viewed 12.1k times · Source

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()?

Answer

NasaGeek picture NasaGeek · Jan 27, 2014

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.