Android 应用程序:在销毁应用程序之前修改 firebase 数据库
Android application: modifying firebase database before app destroyed
我试图在我的应用程序被销毁时修改我的 firebase 数据库,这意味着当我从最近的 运行 应用程序列表中删除该应用程序或当我点击主页按钮时,但我没有知道如何做到这一点,我试图在每个 activity 的 onDestroy() 方法中做到这一点,但它不起作用。
这是我的 onDestroy() 方法:
@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();*/
}
你有没有BaseActivity?
我想你只是忘了在右边添加这一行 activity。
我建议您创建 BaseAvtivity.java class 并从中扩展所有活动,BaseActivity 将扩展 AppCompatActivity,然后覆盖 BaseActivity 中的生命周期方法并在 onDestroy 方法中设置新值。
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.
使用 OnPause
或使用服务写入数据。
在清单中添加这个
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
现在在您的 MyService
服务中,覆盖方法 onTaskRemoved
。 (仅当 stopWithTask
设置为 false
时才会触发)。
public void onTaskRemoved(Intent rootIntent) {
//save data to firebase
//stop service
stopSelf();
}
reference
我试图在我的应用程序被销毁时修改我的 firebase 数据库,这意味着当我从最近的 运行 应用程序列表中删除该应用程序或当我点击主页按钮时,但我没有知道如何做到这一点,我试图在每个 activity 的 onDestroy() 方法中做到这一点,但它不起作用。
这是我的 onDestroy() 方法:
@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();*/
}
你有没有BaseActivity? 我想你只是忘了在右边添加这一行 activity。 我建议您创建 BaseAvtivity.java class 并从中扩展所有活动,BaseActivity 将扩展 AppCompatActivity,然后覆盖 BaseActivity 中的生命周期方法并在 onDestroy 方法中设置新值。
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.
使用 OnPause
或使用服务写入数据。
在清单中添加这个
<service android:name="com.myapp.MyService" android:stopWithTask="false" />
现在在您的
MyService
服务中,覆盖方法onTaskRemoved
。 (仅当stopWithTask
设置为false
时才会触发)。public void onTaskRemoved(Intent rootIntent) { //save data to firebase //stop service stopSelf(); }
reference