Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which actions does the back button/back key on Android trigger?

I am really confused. I have read that the back button

  • calls onDestroy()
  • can close up your currently-running activity
  • calls onPause()

I think onPause() should be right. But this is an side effect, because the Activity gets into the background. I found nothing in the docs. But maybe I have overlooked something.

Can someone please explain to me what the back button is supposed to do programmatically? Some references would also be nice. :-)

like image 845
Flow Avatar asked Sep 02 '11 23:09

Flow


1 Answers

I have read that the back button calls onDestroy(), can close up your currently-running activity, calls onPause()

All three are correct.

I found nothing in the docs.

Quoting the Android documentation:

When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).

To elaborate, if there is nothing else that will consume the BACK button press (e.g., an open options menu), your Activity will be called with onBackPressed(). The default implementation of this calls finish(). This will take your activity from the running to the destroyed states, calling onPause(), onStop(), and onDestroy() in sequence, as shown in the event flow diagram:

enter image description here

like image 131
CommonsWare Avatar answered Oct 13 '22 02:10

CommonsWare