Questions tagged [activity-lifecycle]

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity - the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity - the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

An activity has essentially four states:

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.

enter image description here

Reference: http://developer.android.com/reference/android/app/Activity.html

706 questions
17
votes
2 answers

Android View - What is automatically saved and restored in an Activity

I am a beginner with Android. In Android, some generic elements can be automatically saved/restored in onSaveInstanceState/onRestoreInstanceState. For example, an EditText saves/restores the Text property, a RatingBar saves/restores the Rating…
vernou
  • 6,818
  • 5
  • 30
  • 58
17
votes
2 answers

How to setSupportActionBar in a view that extends LifecycleActivity

I had an Activity that extended AppCompactActivity, and in onCreate method I setted the Toolbar using setSupportActionBar method in the usual way: public class StepMasterActivity extends AppCompatActivity{ @Override protected void…
17
votes
2 answers

Is Activity.onStop() guaranteed to be called (API 11 +)

It is unclear (to me, at least) from the documentation whether Activity.onStop is guaranteed to be called. There are two places with seemingly contradictory information. Javadoc for Activity.onStop: Note that this method may never be called, in…
Villa
  • 459
  • 7
  • 17
15
votes
2 answers

Release resources in onPause instead of onDestroy

This is about POST-honeycomb (i.e., Android 3.0+) and the quotes below come from https://developer.android.com/reference/android/app/Activity.html According to the lifecycle, onStop and onDestroy are killable, this means: Note the "Killable" column…
Captain Obvious
  • 745
  • 3
  • 17
  • 39
15
votes
6 answers

Android - onStop() will be called with a delay

I found my activities onStop() method will be called with a less than 10 seconds delay. I've never seen before this behavior. Note :- The activity is singleTop and it starts with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag. Note :- I'm using Build…
Alex
  • 1,623
  • 1
  • 24
  • 48
15
votes
3 answers

"persistent state" vs. "current state"

Attempting to decide (for my application) what to save in onPause() and what to save in onSaveInstanceState(), I combed the entire SO for hints and clear guidelines. If I understand correctly, onSaveInstanceState() is best for saving "runtime…
14
votes
2 answers

Why is a call to finish() causing onCreate() to be called, starting a new Activity?

(Yes, I've already looked at existing questions related to this problem.) I am calling finish() from my Activity's Up button listener. But although onDestroy() does get around to being called, first onPause() is called and then, surprisingly,…
Alyoshak
  • 2,696
  • 10
  • 43
  • 70
14
votes
1 answer

What is the usage of onCreate method second implementation in Android Activities?

I have alway used onCreate method inside my Activity lifecycle to start or restore from a saved state, but recently found that there is another onCreate method which contains a PersistableBundle: @Override public void onCreate(Bundle…
Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59
14
votes
1 answer

IllegalArgumentException with Otto Event bus in Fragment instance

I am using Otto Event bus to subscribe to certain events in a ListFragment. The bus instance is stored and created in an subclass of Application, in other words, it bus should work as a singleton. It seems like this is not a case... The fragment is…
13
votes
2 answers

onStop vs onDestroy

I have tried to research exactly when the onDestroy method is called for an activity, but I've read some confusing and conflicting information. In general, my question is: under what circumstances is the onDestroy method actually called on an…
Thomas
  • 1,123
  • 3
  • 13
  • 36
13
votes
7 answers

How to finish destroyed Activity

As I understand it, an activity being destroyed is not equivalently to an activity being finished. Finished The activity is removed from the back stack. It can be triggered by the program (e.g. by calling finish()), or by the user pressing the…
Pang
  • 9,564
  • 146
  • 81
  • 122
13
votes
3 answers

Is it safe to do all cleaning up in onDestroy?

More concretely: Is it safe to place the canceling of a task in onDestroy? Also, is it safe to use onDestroy for unregistering receivers and freeing up resources? My aim is to make sure that my task is canceled/destroyed when the Activity is…
13
votes
5 answers

finish() and the Activity lifecycle

I'm learning Android programming for a class, and I have a quick question about how finish() fits into the Activity lifecycle. When you make a call to finish(), what lifecycle callback is started? I presume it's onPause(), then onStop() and…
Karl Giesing
  • 1,654
  • 3
  • 16
  • 24
12
votes
1 answer

ActivityLifecycleCallbacks are not triggered when activity is killed through "Don't keep activities"

In my Android app I have two activities: DemoActivity with a button to start the SearchActivity with an Intent SearchActivity The button is a custom ViewGroup: SearchButton As soon as the SearchButton comes to life it registers for lifecycle…
JJD
  • 50,076
  • 60
  • 203
  • 339
12
votes
3 answers

Android: click event after Activity.onPause()

There are two buttons, button A starts another activity inside its onClickListener using simple Activity.startActivity() method, button B does some other work inside its onClickListener. When I click button B and immediately after button A, then new…
Mariusz
  • 1,825
  • 5
  • 22
  • 36
1
2
3
47 48