Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android back button behaviour

Let's say we have a default, empty activity with default behaviour, launched with default intent flags. User presses back button on the device. The activity disappear... but how, actually?

  • Is pressing back button behaving the same way like finish()?
  • Is the activity immedietely destroyed (onDestroy is called)?
  • Is the activity guaranteed to be destroyed, but not immedietely?
  • Is there any chance the activity won't be destroyed and this instance will be reused when this activity is launched in the future? (so only onPause and onStop -> onStart and onResume are called?)

I'm looking for a reliable answer, so please do not answer if you are not absolutely sure what happens here.

like image 463
Sebastian Nowak Avatar asked Jul 23 '12 10:07

Sebastian Nowak


People also ask

How does the Android back button work?

The Back button appears in the system navigation bar at the bottom of the screen and is used to navigate in reverse-chronological order through the history of screens the user has recently worked with.

Why the Behaviour of the back and up buttons must be different?

The problem is that up and back have different behavior. If a user is at the start destination, the Up button should not be shown. When your app is launched using a deep link on another app's task, Up should take users to the hierarchical parent destination and not back to the other app.

What happens when you press the Back button?

Depending on the user's Android device, this button might be a physical button or a software button. Android maintains a back stack of destinations as the user navigates throughout your application. This usually allows Android to properly navigate to previous destinations when the Back button is pressed.

How can I tell if my Android back button is pressed?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


1 Answers

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

This is a subchapter from the official Android documentation that addresses your question. It is a subchapter of the topic Managing the Activity Lifecycle, which can be read here: http://developer.android.com/training/basics/activity-lifecycle/index.html

It is definitely worth reading the whole chapter to get to know the details about Androids Activity behaviour. But the subchapter ( first link ) is the relevant part to this question.

like image 152
Dennis Avatar answered Nov 04 '22 23:11

Dennis