Questions tagged [android-lifecycle]

Questions regarding the events forwarded by the system to components, during their lifetime, in an Android Application. Most components have a specific LifeCycle that is imposed upon them. This tag is not meant to be used alone: use with the [android-activity], [android-service] and [android-broadcastreceiver] tags.

Questions regarding the events forwarded by the system to components, during their lifetime, in an Android Application. Most components have a specific LifeCycle that is imposed upon them. This tag is not meant to be used alone: use with , , , or

Android Activity Lifecycle

Activity and Fragment Lifecycle Diagram

2414 questions
22
votes
5 answers

In Activity.onCreate(), why does Intent.getExtras() sometimes return null?

This was probably a false alarm, see my own answer. Original question below: An activity has a button that takes the user to another activity. To launch the new activity, we populate our Intent with extras, and onCreate(), the new activity reads…
22
votes
3 answers

Having One Instance Of Activity

I have a problem with Activity navigation and the back stack and I was hoping you could clear it up for me. The Problem Root Activity >>> SecondActivity >> HomeButton This then takes me to the home page and from there I select... Gmail >> Message…
StuStirling
  • 15,601
  • 23
  • 93
  • 150
21
votes
4 answers

No super method getLifecycle() after migrating to androidx

I have been trying to migrate my app to use androidx but I seem to encounter a strange error. From my activity which extends AppCompatActivity when I call getLifeCycle() it throws the following exception Caused by: java.lang.NoSuchMethodError: No…
Abdullah Razzaki
  • 972
  • 8
  • 16
21
votes
4 answers

Easy way to get current Activity, Fragment, LifeCycleOwner from within View?

When writing Views, ViewModels and LiveData are lifecycle aware. The ViewModel want's the current FragmentActivity, LiveData the current LifecycleOwner. You don't know in advance if your View will be wrapped or somewhat. So it requires a flexible…
Blcknx
  • 1,921
  • 1
  • 12
  • 38
21
votes
4 answers

Fragment lifecycle events in LifeCycleAware Fragment

I have a lifecycle aware fragment and a LifecycleObserver class public class MyFragment extends Fragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { …
user4260260
20
votes
2 answers

Should the call to the superclass method be the first statement?

The results of a speech recognition can be read in the onActivityResult(int requestCode, int resultCode, Intent data) method, as shown in this example. This method overrides the same method in class Activity: why is the call to the superclass method…
enzom83
  • 8,080
  • 10
  • 68
  • 114
20
votes
6 answers

details of Android application components destruction and recreation

Could someone push me in the direction of some concrete, trustworthy (and preferably concise) information on the following: The order in which components are destroyed and (where applicable)recreated by the system (Fragment, Activity, Activity's…
kaay
  • 1,083
  • 1
  • 12
  • 31
20
votes
1 answer

When can onTaskRemoved() be called?

The docs state that This is called if the service is currently running and the user has removed a task that comes from the service's application. It seems as if it is only called when the app is swiped away from the recent task list. Pressing…
Steve M
  • 9,296
  • 11
  • 49
  • 98
20
votes
1 answer

Difference between onResume() and onResumeFragments()

FragmentActivity.onResume() javadoc: Dispatch onResume() to fragments. Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed. This means that in…
19
votes
2 answers

Android fragments vs compound controls

Why should Android 3.0 fragments be used instead of compound controls? One can create a View inheritor or compound control once and use it everywhere. I've read http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html but did not…
Anton
  • 1,903
  • 3
  • 16
  • 18
19
votes
3 answers

Android: how to log all Activity methods calls?

I would like to have a quick way to be able to log all the calls to all the super methods called by the activity. I have tried this https://github.com/stephanenicolas/loglifecycle but for some reason it does not work with AppCompatActivity... I…
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
19
votes
3 answers

When to save data to database, onPause() or onStop()?

I know this question has been asked a million times, I myself though that I already knew the answer and that the correct one was that the only guaranteed call is to onPause(), so you should save your data there. However, in many places of android…
klifa
  • 671
  • 1
  • 8
  • 27
19
votes
5 answers

AsyncTask will always run even if app is destroyed?

I have an application and because you can't do network operations on the main thread I'm using AsyncTask, so the question is once I execute() the AsyncTask and right after that I finish() the activity, and maybe the user will finish() the whole app,…
JRowan
  • 6,824
  • 8
  • 40
  • 59
18
votes
2 answers

Android MVVM - How to reference Activity in ViewModel

MVVM architecture, this is my View (Activity): private MyApp app; private MainActivityVM viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = (MyApp)…
AnD
  • 3,060
  • 8
  • 35
  • 63
18
votes
3 answers

Android view - onAttachedToWindow and onDetachedFromWindow - when are they called in the activity lifecycle?

I believe that onAttachedToWindow() is called when the onCreate()'s setContentView(R.layout.myLayout.xml) is called. So can I assume then that in the activity lifecycle that onDetachedFromWindow() is called when the activity is destroyed? My…
j2emanue
  • 60,549
  • 65
  • 286
  • 456