1

I am trying to modify my firebase database when my app is destroyed, that means when I remove the app from the list of recent running app or when I click on Home button ,but I don't know how to do this, I tried to do that in onDestroy() method of every activity but it doesn't work.

This is my onDestroy() method :

@Override
protected void onDestroy() {
    super.onDestroy();
    FirebaseDatabase.getInstance().getReference().child("users").child(encodeEmail(mAuth.getCurrentUser().getEmail())).child("status")
            .setValue("destroyed") ;

    /*Toast.makeText(ContactsActivity.this,"closing app",Toast.LENGTH_LONG).show();
    MyApp app = (MyApp)getApplication() ;
    app.setUpBeforeClosing();*/

}
R15-Zucc
  • 19
  • 1
  • 4
AyoubS
  • 51
  • 8

2 Answers2

0

Do you have BaseActivity or not ? I think you just forgot to add this line in the right activity. I suggest you to create BaseAvtivity.java class and extend all your activities from it, and the BaseActivity would extends AppCompatActivity and then override tbe lifecycle methods in BaseActivity and set new value in onDestroy method.

Rob
  • 886
  • 9
  • 16
  • No I don't have BaseActivity, but I don't know why the onDestroy() is not called when I destroy the activity that contains this onDestroy() method – AyoubS Apr 20 '18 at 20:57
  • try to do this in onPause method instead. – Rob Apr 20 '18 at 21:00
  • I tried it in my onStop method and it's working but I need to execute that when the app is destroyed – AyoubS Apr 20 '18 at 21:17
  • You can write a service and bind it to your application. and do this in the service lifecycle method. and when your app will be closed the service will be destroyed. So that will work with also with activity . you can bind the service to your activity . – Rob Apr 21 '18 at 06:11
  • yeap that's what I did and the problem is solved, thank you – AyoubS Apr 21 '18 at 08:47
0

On Destroy Documentation

Do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Either use OnPause or Use a service to write data.

  1. Add this in manifest

    <service
    android:name="com.myapp.MyService"
    android:stopWithTask="false" />
    
  2. Now in your MyService service, override method onTaskRemoved. (This will be fired only if stopWithTask is set to false).

    public void onTaskRemoved(Intent rootIntent) {
    
    //save data to firebase
    
    //stop service
    stopSelf();  
    }
    

reference

Anik Raj
  • 76
  • 6