My app needs to delete a Firebase document when the user leaves the current Activity, for example, when they press the Home button (go into the background) or the Power button. The following code works fine when Home is pressed, but does nothing when the Power button is pressed:
protected void onPause() {
super.onPause();
// Remove player from players list.
if (mPlayerDocRef != null) mPlayerDocRef.delete();
if (mAuthStateListener != null) mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
Since delete() is an asynchronous action, I tried adding callbacks as follows:
if (mPlayerDocRef != null)
mPlayerDocRef.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
if (mAuthStateListener != null) mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error deleting document", e);
}
});
but the callbacks never get called. Any ideas on what the problem is?
UPDATE: I tested on a real Samsung Galaxy Tab A (Android 9) and Samsung Note 8 (Android 6.0.1) and there's no problem there, only with the Pixel 2 (Android 10). Could be a bug in the OS.