我目前正在开发一个 android 项目,但遇到了一个小问题。我将 firebase 用于数据库,并将数据离线保存到本地存储,但是当我第二次设置 database.setPersistenceEnabled(true);
时,我的应用程序崩溃了。我怎样才能防止这种情况发生?
我在 LoginActivity 中只设置了一次并且我不使用它但是我的应用程序在后台并重新打开应用程序我的应用程序由于持久性而崩溃。
还有一个小问题,在将任何数据保存到 firebase 后,我的 Activity 重新打开。我也不明白。谁能帮帮我?
最佳答案
您需要重写Application
类,将其添加到 list 中,然后调用
database.setPersistenceEnabled(true);
那里。
示例:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}
然后在你的 AndroidManifest.xml
中:
<application
android:name=".MyApplication" <!--Relative path of MyApplication.java-->
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
tools:replace="android:supportsRtl"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>
关于Android Firebase 持久化集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41951292/