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
95
votes
9 answers

How to use onResume()?

Can anyone give me an example that uses onResume() in Android? Also, if I want to restart the activity at the end of the execution of another, which method is executed—onCreate() or onResume()? And if I want to update data, how do I put it in…
Zizou
  • 1,891
  • 3
  • 15
  • 16
94
votes
6 answers

When is the viewmodel onCleared called

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ? A…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
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
85
votes
7 answers

Android: onDestroy() or similar method in Application class

I am extending Application class to work with some global variables that need context. I know there is onCreate() method in the Application class that gets called before any other onCreate() in activities, but I would like to know if there is…
Maybe Julius
  • 1,203
  • 2
  • 13
  • 16
81
votes
7 answers

Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says : An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a …
curioustechizen
  • 10,572
  • 10
  • 61
  • 110
75
votes
2 answers

Android Fragment onCreateView vs. onActivityCreated

I know that a fragment's view hierarchy has to be inflated in onCreateView, but what other functionality can be in onCreateView vs. what should wait for onActivityCreated? My current implementation uses separate activities for everything, and a…
72
votes
3 answers

What is lifecycle for RecyclerView adapter?

I'm requesting images from presenter in adapter: @Override public void onBindViewHolder(SiteAdapter.ViewHolder holder, int position) { Site site = sites.get(position); holder.siteName.setText(site.getName()); …
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
72
votes
2 answers

What the equivalent of activity life cycle in iOS?

Actually, I would say that both iOS ViewControllers and Android Activities have their lifecycle methods. For example an equivalent of ViewController.viewDidLoad() is Activity.onCreate() ? Else I still need to know the equivalent of the other…
TooCool
  • 10,598
  • 15
  • 60
  • 85
72
votes
4 answers

Why use Fragment#setRetainInstance(boolean)?

I find Fragment#setRetainInstance(true) confusing. Here is the Javadoc, extracted from the Android Developer API: public void setRetainInstance (boolean retain) Control whether a fragment instance is retained across Activity re-creation (such as…
Håvard Geithus
  • 5,544
  • 7
  • 36
  • 51
65
votes
7 answers

Jetpack compose - how do I refresh a screen when app returns to foreground

I need to automatically refresh an Android Compose screen when the app returns to the foreground. I have an that requires permissions and location services. If the user has switched any of these off a list is drawn of the items that need to be…
William
  • 1,255
  • 2
  • 10
  • 9
65
votes
5 answers

onActivityCreated is deprecated, how to properly use LifecycleObserver?

Google deprecate fragment’s onActivityCreated() on Android and recommend to use LifeCycleObserver: To get a callback specifically when a Fragment activity's * {@link Activity#onCreate(Bundle)} is called, register a * {@link…
Sever
  • 2,338
  • 5
  • 35
  • 55
65
votes
19 answers

Why is my onResume being called twice?

Basically, this is what I'm doing 1) Set AlarmManager to execute BroadcastReceiver (BCR) Intent intent = new Intent(m_Context, BCR.class); intent.putExtras(extras); PendingIntent pendingIntent = PendingIntent.getBroadcast(m_Context, 0, intent,…
Huy T
  • 1,273
  • 1
  • 11
  • 21
63
votes
9 answers

When Can I First Measure a View?

So I have a bit of confusion with trying to set the background drawable of a view as it is displayed. The code relies upon knowing the height of the view, so I can't call it from onCreate() or onResume(), because getHeight() returns 0. onResume()…
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
55
votes
9 answers

Display fragment viewpager within a fragment

I have a fragment which contains a ViewPager. The ViewPager is associated with an adapter that contains a set of fragments. Upon loading the parent fragment, I am met with an IllegalStateException with the message: java.lang.IllegalStateException:…
52
votes
7 answers

Android lifecycle library ViewModel using dagger 2

I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide. When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the…
TheHebrewHammer
  • 3,018
  • 3
  • 28
  • 45