Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OnBackPressed finish()?

Tags:

android

I'd like to know if it's a good idea to implement in all activities onBackPressed() the finish() function? Or it's not practical and not even good to do it?

If not, what other options besides going to manifest and input the SingleInstance (which I think it makes the application more slow) in every activity, do I have?

Thanks.

like image 576
user2902515 Avatar asked Oct 23 '13 08:10

user2902515


1 Answers

First of all Hardware back button itself calls finish() method. If you are defining onBackPressed() Method in your activity it means you are overriding the default behavior of backButton as onBackPressed() method gets called when you press back button.

Now whether to create a single instance of activity or not depends on your requirement, for e.g. splash screen can be made of a single Instance as it will be visible only once during start of app and there is no need to keep this activity in back stack. The activity you are going to use very often should not be single Instanced otherwise a rare activity can be made a single Instance.

Go through the Back Stack Documentation for complete information.

standard and 'singleTop' can instantiate multiple activity instances and the instance will stay in the same task. For 'singleTask' or 'singleInstance', the activity class uses the singleton pattern, and that instance will be the root activity of a new task.

like image 150
Jitender Dev Avatar answered Oct 31 '22 17:10

Jitender Dev