0

When the app is running foreground, the online will set to true and when the app is in background, the online will set to false. I have this code in all activity:

    @Override
protected void onStart() {
    super.onStart();
    if (mAuth.getCurrentUser() != null) {
        mUserDatabase.child("online").setValue("true");
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (mAuth.getCurrentUser() != null) {
        mUserDatabase.child("online").setValue("false");
    }
}

The problem is when I try to move to another activity the online is always set to false. Why is it always false?

  • https://developer.android.com/guide/components/activities/activity-lifecycle.html – Zun Apr 22 '18 at 09:04
  • I'm confused. Its like the onStart() of the new Activity is first to called then the onDestroy() of the old Activity. –  Apr 22 '18 at 09:12
  • `The problem is when I try to move to another activity the online is always set to false. Why is it always false?` Because of the Activity Lifecycle. Read about onStop in the link I send you. onStop is called the moment the Activity is no longer active – Zun Apr 22 '18 at 09:13
  • Show code where you init or assign value to mAuth, btw mAuth is static or non-static variable in your case? – Son Truong Apr 22 '18 at 09:13
  • @sontruongit I assign it to onCreate() and it is a non-static variable –  Apr 22 '18 at 09:23
  • Put a breakpoint in onStart method of another activity to see mAuth.getCurrentUser() is null or not – Son Truong Apr 22 '18 at 09:55
  • I check it and the result is the mAuth.getCurrentUser() is not null –  Apr 22 '18 at 10:09
  • @sontruongit check firebase realtime database and it turns out that when i go to another activity the online will stay true then after awhile (maybe half second) the online change to false. –  Apr 22 '18 at 10:23
  • You have to use `Lifecyclecallbacks` `onStart()` and `onStop` in `Application` level not in `Activity` level – sagar suri Apr 22 '18 at 14:30
  • https://stackoverflow.com/a/42679191/8327394 this answer will do the job for you. – sagar suri Apr 22 '18 at 14:32

1 Answers1

1

When app is paused ( say minimised by pressing home button ) onPause method should be triggered and when you opens it again it should trigger onPostResume method


Your code should look like below :

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

    // set false

}



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

    // set true

}

Hope it helps you

sandhya sasane
  • 1,334
  • 12
  • 19
  • I will encounter another problem if I try to implement it in onDestroy. When app is running background, the online will stay to true –  Apr 22 '18 at 10:18
  • @xxxQDAxxx, Please let me know what do you mean by app running in background, app never runs in background, its processes do run – sandhya sasane Apr 22 '18 at 13:48
  • app is minimize –  Apr 22 '18 at 13:52
  • It is not windows or linux or desktop application to minimise window.., Its android app and either it can be open and user is doing something in app or it is closed – sandhya sasane Apr 22 '18 at 13:57
  • I mean when you tap the home screen of the phone the app will only minimize and not completely close the app. Once you go back to the app, you will stay on the last activity –  Apr 22 '18 at 14:10
  • @xxxQDAxxx, It does not mean minimise..., It is on Pause event. And Its reverse is onResume, when you opens it from existing opened app in background... And yes now i got what you wants... wait i am editing my answer again and then try it... then it would work – sandhya sasane Apr 22 '18 at 14:17
  • @xxxQDAxxx, Kindly have a look over my previous answer is edited for you.., best luck – sandhya sasane Apr 22 '18 at 14:28
  • I forgot to tell you that im using firebase here and I did check my firebase database if it stay online and yes it stays the online to true then after the new activity is loaded the online change to true –  Apr 22 '18 at 14:28
  • Good answer! Just another problem encounter. when I press the back button of the phone soft key. The online goes to false but I'll try fix that. Thanks for the solution –  Apr 22 '18 at 14:39
  • @xxxQDAxxx, Will you please describe the another problem in very simple style and open another question.., I will surely help as stackoverflow is giving warning that you should not be having too many comments and it reccommends chat – sandhya sasane Apr 22 '18 at 14:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169538/discussion-between-sandhya-sasane-and-xxxqdaxxx). – sandhya sasane Apr 22 '18 at 14:48