gpt4 book ai didi

Android Firebase 持久化集

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:56 28 4
gpt4 key购买 nike

我目前正在开发一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com