简体   繁体   中英

How to show Online of a user?

I'm trying to get an app to show all the users online... I'm using the following way to achieve it

    @Override
public void onStart(){
    super.onStart();
    mDatabaseReference.child("Online").setValue(true);
}

@Override
public void onStop(){
    super.onStop();
    mDatabaseReference.child("Online").setValue(false);
}

I'm not using ondisconnect because it shows offline only if the app is completely closed(not running in the background). SO i used this method on each activity... But the problem is that whenever i open an activity it shows online and the next second turns offline... I'm guessing that its because the prev activity closes after opening the new activity so the presents activity's on start is executed before the next activity's on stop. So since the activity's onstop is executed last it shows offline. How do i solve this problem

Your guess is completely correct.

Starting a new Activity will cause the onPause() and onStop() methods to be called for the first Activity.

Based on your description, I'm assuming that you want the Online status to remain true for as long as the app remains in the foreground and you are putting it in every Activity because your app doesn't only open from a single main Activity.

Your current code will work without a problem if you switched to using a Single-Activity Architecture, which is simply to use a single Activity and have it display different Fragments instead of new Activities. This solution will work with your existing code because onStop() and onStart() will only be called when your app enters the background.

If you look at the Navigation section in the official Android Developers Blog, you'll see that Google wants to encourage developers to switch to using the Single-Activity Architecture.

However, if you still wish to use multiple Activities, then you might want to consider an alternative way to keep track of Online status to take into account of multiple Activities.

For example, rather than using a simple boolean value, you can use an int value.

@Override
public void onStart(){
    super.onStart();
    mDatabaseReference.child("Online").addValue();
}

@Override
public void onStop(){
    super.onStop();
    mDatabaseReference.child("Online").removeValue();
}

With the addValue() and removeValue() being:

private void addValue(){
    activityCount++;
    onlineStatus = true;
}

private void removeValue(){
    activityCount--;
    if(activityCount <= 0)
        onlineStatus = false;
}

Please keep in mind that this is only an example and doesn't take into account of how your app is designed. Bottom line is, you'll have to think of a solution that takes into account of multiple Activities that are displayed.

I do heavily suggest the Single-Activity approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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