2

So Basically what I want to do is simply change some values in the database when an App is minimized or Stopped!

This is how my code looks like:

  @Override
    protected void onStop() {
        super.onStop();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if(currentUser != null) {

            mUserRef.child("online").setValue(ServerValue.TIMESTAMP);

        }

    }

But the problem is when I call startActivity(startInten) i.e move to second intent this method is called at that time as well. I have researched a lot on the internet. Can anyone tell me which activity lifecycle method is the one which is only called when the app is minimized and killed and not when we intent to other activity within the same app?

Thanks.

Rehan
  • 454
  • 7
  • 19

2 Answers2

4

Another reliable way to handle this would be to use a Service for this that executes your task when the app is closed. This could be used like this:

Service class:

public class CloseService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) { 
        //use the relevant code here that should be executed when the app is closed
        stopSelf();
    }
}

Also it's needed to register the Service in manifest.xml like this:

<manifest
    <application  
        </activity>

        <service android:name=".CloseService"/>

    </application>
</manifest>
jle
  • 686
  • 1
  • 8
  • 24
3

You should use ProcessLifecycleOwner from android lifecycle library and observe it from application class onCreate method.

Sample code (in kotlin):

ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleObserver{
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        fun onStop(){
            val currentUser = mAuth.getCurrentUser()
            if (currentUser != null) {
                mUserRef.child("online").setValue(ServerValue.TIMESTAMP)
            }
        }
    })

Sample code (in Java):

ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onStop(){
            FirebaseUser currentUser = mAuth.getCurrentUser();
            if(currentUser != null) {
                mUserRef.child("online").setValue(ServerValue.TIMESTAMP);
            }
        }
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onStart(){
            //onstart action here
        }
}

This is going to get called when the last activity has called onStop and app has moved to background or killed.

user3783123
  • 544
  • 3
  • 15
  • @Rehan You can find more detailed explanation of ProcessLifecycleOwner in this proandroiddev post: https://proandroiddev.com/android-processlifecycleowner-by-example-2f965061b9da – user3783123 Oct 10 '19 at 13:53
  • @Rehan Edited my answer with sample code in Java. Also you can take a look at this post: https://medium.com/@gbalbhadra1994/android-check-whether-your-app-is-in-background-or-foreground-using-processlifecycleowner-3258fcc07c5 P.S You should be learning Kotlin as it's now official language for Android development. – user3783123 Oct 10 '19 at 14:05
  • @Rehan Did you add the android.arch.lifecycle:extensions:1.1.1" to your build.gradle? And be sure you added it to the app module not project. – user3783123 Oct 10 '19 at 14:17
  • Yes. It is compiling if i put it in some method. – Rehan Oct 10 '19 at 14:18
  • 1
    You should put the sample code in your onCreate method. – user3783123 Oct 10 '19 at 14:19
  • Thanks, it worked! Only Issue was that value doesnt change if I go from foreground back to the other activity than the one which has onStart Initialization – Rehan Oct 10 '19 at 14:30
  • @Rehan I edited the Java sample code to include onStart event handling. – user3783123 Oct 10 '19 at 14:37
  • I want to share a problem after implementing the code. I am getting Null pointer exceptions in my fragments which are associated with activity when a user is null or not logged in after adding the above code in oncreate method. – Rehan Oct 15 '19 at 17:12
  • 1
    `OnLifecycleEvent` is deprecated. Please see this answer for an alternative: https://stackoverflow.com/a/70808707/3422470 – James Jan 21 '22 at 23:39