noHistory vs finish() - Which is preferred?
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.
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.