简体   繁体   English

活动生命周期。 用户始终离线

[英]Activity lifecycle. User always offline

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. 当应用程序在前台运行时,在线将设置为true;当应用程序在后台运行时,在线将设置为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. 问题是,当我尝试进行其他活动时,在线总是设置为false。 Why is it always false? 为什么总是虚假?

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 当应用暂停时(例如,通过按下主屏幕按钮最小化),应触发onPause方法,当您再次打开它时,应触发onPostResume方法


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 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM