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
32
votes
7 answers

Failed to resolve: android.arch.lifecycle:extensions:1.0.0-alpha1 android studio 3.0

I am building a project in android studio 3.0 with android plugin 'com.android.tools.build:gradle:3.0.0-alpha1' and gradle version gradle-4.0-milestone-1-all.zip. Used maven repo as well: maven { url 'https://maven.google.com' …
31
votes
3 answers

DialogFragment - retaining listener after screen rotation

I have a DialogFragment which creates a DatePickerDialog. I'm using a static method called newInstance to set the initial values in order to use the default empty constructor. However, how am I supposed to set the listener? After the screen…
Renato Rodrigues
  • 1,038
  • 1
  • 18
  • 35
30
votes
3 answers

ViewModel has no zero argument constructor hilt Java

I am using hilt and I am trying to import my SessionManager into my AuthViewModel class. When I am taking a look at Google's documentation on hilt with ViewModel. We can see they are able to inject ExampleRepository repository into the constructor…
user7922501
  • 420
  • 1
  • 5
  • 12
30
votes
1 answer

Can I get the instrumentation test to kill and restart the app process?

I need to test a use case where the application starts from a clean state - i.e. the process has not been running before the test starts. From what I see from logcat, all instrumentation tests run under one single process instance/session, so the…
Nilzor
  • 18,082
  • 22
  • 100
  • 167
30
votes
6 answers

Properly skip login activity if already logged in

My launcher icon currently starts the login activity. I've stored the logged in status in SharedPreferences. Is there any way to properly skip the login activity and go straight to the main activity without any UI glitches. All existing solutions…
Monstieur
  • 7,992
  • 10
  • 51
  • 77
29
votes
2 answers

ViewPager Fragments are not recreated after FragmentTransaction.replace() followed by back button

I am trying to implement saving and restoring state, but I am running into problems when replacing the main Fragment with a PreferenceFragment and then hitting the back button. My main Fragment consists of a ViewPager with a FragmentPagerAdapter…
28
votes
5 answers

Fragment lifecycle with respect to it's activity

Situation My activity waits on an Async operation and after it hears back from async operation, it needs to pass information to 2 fragments inside it. Requirement 1. Both fragments need their onCreateView calls to be done for them to have their…
Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
28
votes
5 answers

When to use and when not to use a Service in Android

I have been developing for Android for little less then 2 years, and I am still puzzled by this seemingly simple question. When should one implement a service? From my experience there are some rare cases but I am questioning this because on every…
Igor Čordaš
  • 5,785
  • 4
  • 42
  • 54
28
votes
3 answers

Public static variables and Android activity life cycle management

According to the documentation the Android OS can kill the activity at the rear of the backstack. So, say for example I have an app and open the Main Activity (let's call it Activity A). In this public activity class I declare and initialize a…
jsstp24n5
  • 594
  • 1
  • 6
  • 13
27
votes
2 answers

When is the savedInstanceState bundle actually used?

Does anyone know of an exhaustive list of when the savedInstanceState bundle will be used in an activity? I know it's used when the device orientation changes. However, it doesn't seem to be used when the user force closes the app from the Android…
howettl
  • 12,419
  • 13
  • 56
  • 91
27
votes
3 answers

Reacting to activity lifecycle in ViewModel

I'm trying to create an app which will use MVVM architecture and there's one thing I quite don't understand. Official Android docs say that's not a good idea to reference activity context in ViewModel's (as ViewModel may outlive activity) so I've…
27
votes
1 answer

Android: finishAffinity() vs finishAndRemoveTask()

TLDR: What is the difference between finishAffinity() and finishAndRemoveTask()? I am working on an Android app that has one single activity, and uses fragment switching instead of new activities. I was having a weird issue with a certain fragment…
Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38
27
votes
5 answers

Android scenario where ondestroy() is called without onpause() or onstop()

A few days back I was asked to write down scenarios where ondestroy() is called without onpause() or onstop() being called. Is it possible. If yes please explain.
Hemant Parmar
  • 426
  • 1
  • 5
  • 12
27
votes
1 answer

What is the difference between setContentView and LayoutInflater?

I am creating a tabs list with several fragments. I have noticed that, in the main activity, I used setContentView to get the layout xml and use findViewById to get the corresponding UI element…
user782104
  • 13,233
  • 55
  • 172
  • 312
26
votes
5 answers

Register and Unregister the BroadcastReceiver in Application Class

I have a broadcast receiver Which is registered in the onCreate() method of Android Applcation class but How to unRegister the same example public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); …
Anbu
  • 671
  • 1
  • 6
  • 18