Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

noHistory vs finish() - Which is preferred?

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

like image 764
Gokul Nath KP Avatar asked Jan 27 '14 07:01

Gokul Nath KP


People also ask

How do I finish my current activity and start a new one?

Use finish like this: Intent i = new Intent(Main_Menu. this, NextActivity. class); finish(); //Kill the activity from which you will go to next activity startActivity(i);

What is finish affinity in Android?

finishAffinity(): finish the current activity and all activities immediately below it in the current task that have the same affinity. finishAndRemoveTask(): call this when your activity is done and should be closed and the task should be completely removed as a part of finishing the root activity of the task.

What is finish method?

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations. That means that if at least one batch wasn't executed we will not enter finish() method.


2 Answers

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.

like image 66
NasaGeek Avatar answered Oct 27 '22 09:10

NasaGeek


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.

like image 37
Carlos Robles Avatar answered Oct 27 '22 09:10

Carlos Robles