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
12
votes
3 answers

Can Android 'kill' an Activity without killing the application?

As we know, the default flow in Android for such scenario is calling the activity's respective onSaveInstanceState, onStop, onDestroy methods before releasing the reference to the Activity object. However it appears I have a case when my application…
George
  • 3,727
  • 9
  • 31
  • 47
12
votes
1 answer

Lifecycle of a session cookie in an Android WebView / CookieSyncManager

I have an Android application which makes requests to my webserver via both a WebView and an HttpClient. I sync cookies between the two using a CookieSyncManager. So far, so good. When my application starts (inside onResume()), I run a piece of…
gnmerritt
  • 566
  • 1
  • 6
  • 14
12
votes
5 answers

Is there a function in Android analogous to "int main" in C/C++ which contains the program's main loop?

Normally in a C or C++ program there's a main loop/function, usually int main (). Is there a similar function that I can use in android Java development?
RyanCheu
  • 3,522
  • 5
  • 38
  • 47
11
votes
1 answer

Activity Life Cycle

I'm trying to understand An Activity full life-cycle. So I have searched on Google and found lots of tutorials regarding activity life-cycle, but in all tutorials I have not found these methods in life-cycle diagram: 1. OnContentChanged() 2.…
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
11
votes
3 answers

Where can be use onSaveInstanceState where parameters are passed different Activity?

For Acitivity there is already a method called onSaveInstacestate(Bundle) which used to store the data of activity which is overridden method. As i see, there are two different onSaveInstanceState where parameters are passed differently as…
Swift
  • 271
  • 3
  • 13
11
votes
1 answer

Dagger 2 Save and Restore State when Activity Stops

I'm at an impasse. I'm using Dagger 2 for dependency injection, but I'm losing state when the app goes into the background. Here is the scenario: the app starts up and creates the dependencies. All works perfectly as long as the app stays in the…
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
11
votes
5 answers

Should I manually close HandlerThreads created by my application when destroying the activity?

My app is composed of a single Activity. In this activity, I'm creating multiple HandlerThreads which run in a loop to do socket blocking operations. Currently I post a quit message to everyone of these HandlerThreads during my…
Daniel L.
  • 5,060
  • 10
  • 36
  • 59
11
votes
4 answers

Why would Application sometimes restart on killProcess?

Ordinarily, exiting my application by calling: android.os.Process.killProcess(android.os.Process.myPid()); performs well without incident. But every once in a while, the application will restart again (after exiting!). The relevant log snippet…
ef2011
  • 10,431
  • 12
  • 49
  • 67
10
votes
2 answers

Activity onStop() not called when home button is pressed in Android N multi window mode

I am trying to make our video app to support Android N multiwindow mode. I have discovered that activity lifecycle becomes confused in multiwindow mode. The phenomenon is when our app layouts on the top screen with the whole screen in portrait, then…
10
votes
3 answers

BufferQueue has been abandoned: When playing video with TextureView

Every time I pause my activity (actually Fragment) to go to another app, upon returning with onResume I try to resume the video playing but it does not play: I get a blank screen. Upon investigation, I see the following in the…
learner
  • 11,490
  • 26
  • 97
  • 169
9
votes
2 answers

Activity cannot be converted to LifecycleOwner

I would like to use Room with LiveData, and in other projects I already used it, but in this one, I can not get it to work. It can't convert my activity into Lifecycle activity when I try to observe the livedata, however, I'm using the…
9
votes
4 answers

Android App, Activity State (Running, Not Running, Foreground/ Background)

I have come across a requirement but I am not able to get the correct way of implementation and hence need your help. What I want to do? - I want to perform an action depending upon notification I get as follows: When app is open and in foreground…
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
9
votes
0 answers

the onStop() not excute immediately after onPause() has done?

Once I finish the activity,the method of onPause() excute immediately.but the method of onstop() coming with delay.Just about after 10 sec,the onstop() can be excute. why and how can I solve it? Result:
rade.chan
  • 93
  • 5
9
votes
1 answer

Activity re-creation intent extras are null

My app contains a MainActivity and uses full screen fragments to display content. I'm trying to achieve the following during app re-creation (When the app has been in background for a long time, gets killed by the system and then it's brought to the…
9
votes
3 answers

Why does calling getWidth() on a View in onResume() return 0?

Everything I've read says you can't call getWidth() or getHeight() on a View in a constructor, but I'm calling them in onResume(). Shouldn't the screen's layout have been drawn by then? @Override protected void onResume() { super.onResume(); …
1 2
3
47 48