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
402
votes
16 answers

Removing an activity from the history stack

My app shows a signup activity the first time the user runs the app, looks like: ActivitySplashScreen (welcome to game, sign up for an account?) ActivitySplashScreenSignUp (great, fill in this info) ActivityGameMain (main game screen) so the…
Mark
  • 39,551
  • 15
  • 41
  • 47
243
votes
18 answers

On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activities

All activities in my application require a user to be logged-in to view. Users can log out from almost any activity. This is a requirement of the application. At any point if the user logs-out, I want to send the user to the Login Activity. At this…
skyler
  • 8,010
  • 15
  • 46
  • 69
174
votes
13 answers

What is Activity.finish() method doing exactly?

I'm developing android applications for a while, and followed a lot of posts about activity life cycle, and application's life cycle. I know Activity.finish() method calls somewhere in the way to Activity.onDestroy(), and also removing the activity…
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
86
votes
4 answers

Is onResume() called before onActivityResult()?

Here is how my app is laid out: onResume() user is prompted to login If user logs in, he can continue using the app 3. If the user logs out at any time, I want to prompt login again How can I achieve this? Here is my MainActivity: @Override …
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
77
votes
13 answers

Activity lifecycle - onCreate called on every re-orientation

I have a simple activity that loads a bitmap in onCreate. I find that if I rotate the device I can see from the logs that onCreate called again. In fact, because all instance variables are set to default values again I know that the entire Activity…
Synesso
  • 37,610
  • 35
  • 136
  • 207
50
votes
2 answers

Why implement onDestroy() if it is not guaranteed to be called?

According to the android Activity Lifecycle, the only callback guaranteed to be called (if an activity ever leaves the Running state, which is typically expected) is onPause(). So, I must assume that there are scenarios in which it makes sense to…
uTubeFan
  • 6,664
  • 12
  • 41
  • 65
47
votes
7 answers

OnPause and OnStop() called immediately after starting activity

I have an activity that needs to turn screen on(if offed) when it is started. So in onCreate, I have: this.getWindow().setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |…
Haris
  • 1,822
  • 2
  • 22
  • 44
44
votes
3 answers

Method to override when layout is destroyed in Android

I have a custom component which extends LinearLayout, I need to execute certain statements when Layout is destroyed or removed. (or about to be removed) One way is to check for onPause() or onDestroy() of an activity and call methods of the custom…
sat
  • 40,138
  • 28
  • 93
  • 102
42
votes
8 answers

How to simulate killing activity to conserve memory?

Android doc say: "When the system, rather than the user, shuts down an activity to conserve memory, ... " But how to simulate this situation?I want to debug the onRestoreInstanceState(Bundle) method,but don't know how to.
L.J.W
  • 1,575
  • 5
  • 18
  • 24
42
votes
6 answers

Life cycle of Android Activity after pressing Back button

I am little confused between the life cycle of two activities. Suppose I have Activity A and Activity B. B is called From A i.e A ----> B. Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still…
AJ.
  • 4,526
  • 5
  • 29
  • 41
34
votes
3 answers

Is it mandatory to remove yourself as an observer from Android Lifecycle?

I am building an Android Java class which implements the LifecycleObserver interface. This is the constructor: public MyObserver(AppCompatActivity activity) { this.mActivity = new WeakReference(activity); …
esilver
  • 27,713
  • 23
  • 122
  • 168
27
votes
7 answers

Simulate killing of activity in emulator

I would like to test out onSaveInstanceState and onRestoreInstanceState for my app on the emulator. I have found this, which says that we could simulate this during orientation change, but I'm storing some of my variables on the application level…
SteD
  • 13,909
  • 12
  • 65
  • 76
27
votes
1 answer

What is the proper way to unregister Activity lifecycle callbacks?

In the Android Application class you can register/unregister ActivityLifecycleCallbacks, which allow you to monitor Activity lifecycle events in your application all in one place (if you so desire). Where is the proper place to call…
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
24
votes
4 answers

How do you build an Android back stack when an activity is started directly from a notification?

I have two activities: Activity A - list of items Activity B - detail view of an item Normally, a user opens the app and Activity A is launched. A user sees a list of items, clicks one, and Activity B is started to display the item detail. Activity…
Andrew C
  • 1,036
  • 2
  • 9
  • 19
18
votes
3 answers

Prevent Android activity from being recreated on turning screen off

How to prevent an activity from being recreated on turning screen off? What I do Start Bejewels and go to the jewelry screen. Press power button shortly. The screen is turned off, but the device is not. Press power button again. What I see The…
1
2 3
47 48