Skip to content Skip to sidebar Skip to footer

How Can I Keep Status 'online' In Android

Now i'm trying to make like a instagram chat program using firebase the problem is i want to show that member login status but android life cycle is problem Here's my code HomeActi

Solution 1:

Put this dependency in your build.gradle file:

implementation "android.arch.lifecycle:extensions:*"

Then in your Application class, use this:

publicclassMyApplicationextendsApplicationimplementsLifecycleObserver {

    @OverridepublicvoidonCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)privatevoidonAppBackgrounded() {
        Log.d("MyApp", "App in background");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)privatevoidonAppForegrounded() {
        Log.d("MyApp", "App in foreground");
    }
}

Update your AndroidManifest.xml file:

<applicationandroid:name=".MyApplication"....></application>

When your app is in background change status to offline and when app is in foreground change it's status to online.

Reference

Solution 2:

The problems look like your ChatsActivity is destroyed while Glide is trying to load the image. You can use getApplicationContext() to get the current Context. Try to replace,

Glide.with(ChatsActivity.this).load(user.getImageurl()).into(image_profile);

with

Glide.with(getApplicationContext()).load(user.getImageurl()).into(image_profile);

Also, you should use .onDisconnect() method given by firebase to check the presence of the user

When you establish an onDisconnect() operation, the operation lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the your app if it is invalid. The server then monitors the connection. If at any point the connection times out, or is actively closed by the Realtime Database client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event.

You can find out more from this doc

Solution 3:

Whenever user is in your app, technically he is online. So in your home activity use this:

@Override
    protected void onStart() {
        super.onStart();
        myRef.child(uid).child("isOnline").setValue(true);
        myRef.child(uid).child("isOnline").onDisconnect().setValue(false);
    }

and also i would suggest to add to override onResume():

@OverrideprotectedvoidonResume() {
        super.onResume();
            myRef.child(uid).child("isOnline").setValue(true); //User back online

        }

Make sure to add an if statement to check if user is connected to internet or not!

Post a Comment for "How Can I Keep Status 'online' In Android"