How can I delete a Firebase document when user presses the Power button

FractalBob :

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.

Blundell :

onPause()

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode). Use the onPause() method to pause or adjust operations that should not continue (or should continue in moderation) while the Activity is in the Paused state, and that you expect to resume shortly.

Turning off the phone I guess could technically it won't be resuming shortly. I know some phones have "turning off" animations and in those cases, you will get an onPause.

But try onStop or onDestroy also:

onStop()

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running, and is about to be terminated.

On destroy I am less convinced about as the docs say it's not guaranteed to be called:

onDestroy()

onDestroy() is called before the activity is destroyed. The system invokes this callback either because:

  • the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or the
  • system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

You can also register a Broadcast Receiver for shutdown events:

    <intent-filter>
        <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
        <action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>
    </intent-filter>

BroadcastReceiver's behaviour for ACTION_SHUTDOWN

Try registering the BroadcastReceiver programmatically also with Activity.registerReceiver() that way you will get the callback in your Activity.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18295&siteId=1