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
26
votes
5 answers

Activity life cycle methods : onPostResume significance

Official documentation about Activity lists out 7 life cycle methods. onPostResume() was not quoted as life cycle method. But I feel that this method is important method. During the life cycle, when an activity is visible from hidden to show…
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
26
votes
6 answers

Android - Preserve or delete files created by the application on uninstall

I created an application which creates and stores files to sdcard. Is there a way to bind the folder with application in order to delete all files when the user runs uninstall on android device?
25
votes
4 answers

How to execute background task when Android app is closed / set to background?

My Android 4+ app is connected to a custom web service that is used to sync data every few minutes. To make sure, that the online data is always up to date, I want to trigger the sync when ever the app is closed / send to background. Under iOS this…
25
votes
4 answers

Best way to implement Socket.io in android

I am planning to implement Socket.io in android by this library for a chat based application. As far as I understood the library seems to be pretty good. I want to know how to maintain a single socket connection throughout the app all the time? …
ImMathan
  • 3,911
  • 4
  • 29
  • 45
25
votes
7 answers

Misbehaviour of Backstack of activity when activity destroyed

I have two activities; let's say A and B. In activity A there is a broadcast receiver registered that listens for a particular event which will finish activity A. I am registering the broadcast receiver in onCreate(), and destroying it in…
24
votes
1 answer

Android Studio doesn't let me use repeatOnLifecycle

I want to observe data inside my fragment from viewModel, but Android Studio keeps triggering this warning. Can someone help with this problem? Can this problem somehow be related to Android Studio Bumbleblee's update?
24
votes
3 answers

What is the difference between requireActivity and onActivityCreated

In my code I was inconveniently loading pictures after onCreateView() because I was not sure if the activity was available yet. Because Glide required an activity context I placed the section of code into onActivityCreated(): override fun…
chrisdottel
  • 1,053
  • 1
  • 12
  • 21
24
votes
4 answers

what is the fragment equivalent of Activity.isFinishing()?

In my activities I frequently use this idiom: @Override public void onDestroy() { super.onDestroy(); if (isFinishing() != true) return; // do some final cleanup since we're going away for good } Fragment has an onDestroy() method, but…
jph
  • 2,181
  • 3
  • 30
  • 55
23
votes
2 answers

Why is onDestroy always called when returning to parent activity?

I have a very simple app based on the Building Your First App tutorial. There are two activities: MainActivity invokes DisplayMessageActivity through startActivity(). When entering DisplayMessageActivity, I see: MainActivity.onStop() as expected,…
mtoossi
  • 937
  • 8
  • 14
23
votes
3 answers

Prevent multiple instances of my Android application composed of a single activity

I have an Android application which is composed from a single Activity. How can I assure that only one instance of my application (== Activity) will exist in a given time? I got to a situation in which I succeeded to open multiple instances of my…
Daniel L.
  • 5,060
  • 10
  • 36
  • 59
22
votes
3 answers

android: when to use onStart(), onStop()?

I've read several posts that describe the difference between onStart() and onResume(): onStart() is called when the activity becomes visible, onResume() is called when the activity is ready for interaction from the user. fine. I've always just added…
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
22
votes
2 answers

getViewLifeCycleOwner() vs 'this' vs this.getActivity() in Androidx Fragments

We utilize LiveData's observe method quite a bit in our fragments. A recent release of the androidx fragment sdk is leading to Android Studio marking instances of liveDataObject.observe(this) as incorrect in favor of…
user1114
  • 1,071
  • 2
  • 15
  • 33
22
votes
3 answers

Main difference between Manifest and Programmatic registering of BroadcastReceiver

I am trying to understand the main differences between registering a BroadcastReceiver in the Manifest and registering it programmatically... My understanding is basically as follows (would appreciate someone correcting my points if I am missing…
rwm
  • 241
  • 2
  • 4
22
votes
6 answers

Why Fragment's onCreate() is sometimes called prior to Activity's onCreate()?

Recently I came across a hard to reproduce issue. The NPE occurs when a fragment tries to initialize ArrayAdapter with the data from Activity. The default list initialized in Activity's onCreate method: @Override protected void onCreate(Bundle…
k_shil
  • 2,108
  • 1
  • 20
  • 25
22
votes
2 answers

Can Wi-Fi or the CPU go to sleep when screen is on?

There are WakeLocks and WifiLocks on Android -- but do I need these if my screen is never turning off? I'm using persistent connections (e.g. WebSockets) for dozens of minutes or even hours. Will my screen being always on be sufficient to prevent…
caw
  • 30,999
  • 61
  • 181
  • 291