Android back button behaviour
Asked Answered
H

5

11

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.

Holmquist answered 23/7, 2012 at 10:58 Comment(6)
this might be of your interest: developer.android.com/training/basics/activity-lifecycle/…Lovett
can you post your code, so we can judge betterDownward
Is there something in default, empty activity with default behaviour, launched with default intent flags you can't reproduce? Just create a default project in your IDE.Holmquist
@Chips_100: The link you provieded has no answer to this question.Holmquist
Read this subchapter: developer.android.com/training/basics/activity-lifecycle/… [...]your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().[...] I would recommend to read the whole chapter though.Lovett
Okay, thanks. So the activity is always destroyed and won't be reused. Could you post it as an answer, so I can accept it?Holmquist
L
6

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.

Lovett answered 23/7, 2012 at 14:15 Comment(1)
This is a link-only answer, can you please quote the relevant parts of the doc? It feels like you want to do good by forcing people to read those articles, try the Socratic method and use the spoiler markdown.Quicksilver
F
2

When you press back, (if not intercepted by anything like the keyboard, fragment, activity, etc) the OS (via ActivityManager probably) will try to show the user the previous activity in your current task (again, ignoring fragments' back stack).

If there is no such activity, the task will be terminated and you'll go to the previous task - the home screen most of the times or some other application that might have launched your app.

You'll get onDestroy called soon (it depends on how long it takes to start the next activity but on a good phone it should be under 100-200ms).

Your activity instance won't be reused after onFinish. This happens before the activity is destroyed so if you need another activity of the same type, the OS will create another instance.

Faggot answered 23/7, 2012 at 11:10 Comment(3)
That's not what I've asked about. The question is precise enough.Holmquist
"The activity disappear... but how, actually?" - I think I addressed your questionFaggot
And that's the problem with you. You just read the first sentence and answer without thinking. If you would read the rest, you would notice that I've explained what exactly am I asking about.Holmquist
S
2

you use should look into this try this

and please tell specific what you wish to do with back button for your default activities ......

Sosthena answered 23/7, 2012 at 11:20 Comment(0)
G
1

When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is guaranteed to be destroyed, but not immediately, may be when the system resources are low) and the previous activity resumes (the previous state of its UI is restored).

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

Gillette answered 23/7, 2012 at 11:9 Comment(2)
Please refer to this question: #4779254 If it's always destroyed, then why do they force finish() on back button?Holmquist
Also check out the accepted answer here: #3249832Holmquist
L
0

Definitly onDestroy() is called .....There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

Layette answered 14/1, 2013 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.