简体   繁体   中英

onStop() online status issue - home screen vs previous activity

im using the onStop method to determine when the app closes from the home activity (specifically using either the home button on device or task manager button on device) it doesnt set a timestamp to signify the user was last online at (timestamp) in the firebase database. however, when onStop is called for all other activities it remains as online = true, which is correct.

// Home button pressed + task button pressed + leaving activity
@Override
protected void onStop() {
    super.onStop();
    FirebaseUser currentUser = mFirebaseAuth.getCurrentUser();
    if (currentUser != null) {
        final String currentDate = DateFormat.getDateTimeInstance().format(new Date());
        mUserDatabase.child("Online").setValue(currentDate);
    }
}

Is there any way i can specify if onStop is called from home activity, option a) will put timestamp in the database, or, option b) does not change online status on database (as user is still in app, but on another activity).

you can create a class like this :-

public class MalikChat extends Application {

    private DatabaseReference mUserDatabase;
    private FirebaseAuth mAuth;

    @Override
    public void onCreate() {
        super.onCreate();
        Fabric.with(this, new Crashlytics());
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

        mAuth = FirebaseAuth.getInstance();

        if (!isUserLoggedOut(this)) {
            if (mAuth.getCurrentUser() != null) {
                mUserDatabase = FirebaseDatabase.getInstance().getReference()
                        .child(onlineStatusTable).child(mAuth.getCurrentUser().getUid());

                mUserDatabase.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot != null) {
                            mUserDatabase.child("status").onDisconnect().setValue(String.valueOf(System.currentTimeMillis()));
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            } else {
                Toast.makeText(this, "Your session is expired, Please login again", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                setUserLoggedOut(this, true);
            }
        }
    }
}

or in your manifest file :-

<application
        **android:name=".offlineHalper.MalikChat"**
        android:allowBackup="false"
        android:icon="@drawable/ic_chat_group"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_chat_group"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

if you want to set user status then set this method in your all activities like this :-

@Override
    protected void onStart() {
        userStatus("online");
        super.onStart();
    }

    @Override
    protected void onPause() {
        userStatus(String.valueOf(System.currentTimeMillis()));
        super.onPause();
    }

userStatus Method :-

public static void userStatus(String status) {
        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference(onlineStatusTable).child(firebaseUser.getUid());
        try {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("status", status);
            reference.updateChildren(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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