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.

Sebastian Nowak
  • 5,607
  • 8
  • 67
  • 107
  • 4
    this might be of your interest: http://developer.android.com/training/basics/activity-lifecycle/index.html – Dennis Jul 23 '12 at 10:59
  • can you post your code, so we can judge better – Rahul Baradia Jul 23 '12 at 11:05
  • 1
    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. – Sebastian Nowak Jul 23 '12 at 11:05
  • 1
    @Chips_100: The link you provieded has no answer to this question. – Sebastian Nowak Jul 23 '12 at 11:09
  • 1
    Read this subchapter: http://developer.android.com/training/basics/activity-lifecycle/recreating.html `[...]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. – Dennis Jul 23 '12 at 11:12
  • 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? – Sebastian Nowak Jul 23 '12 at 11:20

5 Answers5

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.

Dennis
  • 14,210
  • 2
  • 34
  • 54
  • 1
    This is a [link-only answer](http://meta.stackexchange.com/q/8231/247470), 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](http://meta.stackexchange.com/q/1191/247470). – TWiStErRob Mar 02 '16 at 11:04
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.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
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 ......

Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
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?

Community
  • 1
  • 1
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Please refer to this question: http://stackoverflow.com/questions/4778754/kill-activity-on-back-button If it's always destroyed, then why do they force finish() on back button? – Sebastian Nowak Jul 23 '12 at 11:16
  • Also check out the accepted answer here: http://stackoverflow.com/questions/3249332/did-back-key-destroy-an-activity – Sebastian Nowak Jul 23 '12 at 11:17
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().

AMIC MING
  • 6,306
  • 6
  • 46
  • 62