noHistory vs finish() - Which is preferred?
Solution 1
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.
Solution 2
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.
Related videos on Youtube
![Gokul Nath KP](https://i.stack.imgur.com/v8FBa.png?s=256&g=1)
Gokul Nath KP
A curious self-learner, with innovative and out-of-box mindset. Looking forward to learn everyday!
Updated on June 12, 2022Comments
-
Gokul Nath KP about 2 years
I don't want my application to show few
Activity
(saySplashScreenActivity
) when pressingback
button. So I've usednoHistory=true
in myManifest.xml
for thatActivity
as show below:<activity android:name="com.gokul.SplashScreenActivity" android:noHistory="true" > </activity>
Instead of setting
noHistory
, I can also callfinish()
in mySplashActivity.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 callfinish()
?-
Marian Paździoch almost 10 yearsPlease 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
-
-
Kaizie about 7 yearsI 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.
-
Cody about 4 yearsIt'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.