在哪里放Firebase.setAndroidContext()函数 [英] Where to put Firebase.setAndroidContext() function
问题描述
我想玩一点Firebase和Android。
我有一个 RegisterActivity
和一个 MainActivity
。
我目前的流程是 - 以 MainActivity
开头 - 检查用户是否注册,如果没有,调用 RegisterActivity
- 注册后调用 MainActivity
。
我无法将 Firebase.setAndroidContext()
调用。
我有两个问题:
- 只在
应用程序
或者每个活动
? - 如果问题1的答案只有一次,那么我应该把它放在哪里?
谢谢大家,
Giora。 Firebase快速入门文档:
$ b
Firebase库必须初始化一次与Android
上下文
。在创建或使用任何Firebase引用之前,这必须发生。
更新名称 I'm trying to play a bit with Firebase and Android. I'm having trouble with where to put the Thank you all,
Giora. To quote (step 4 of) the Firebase quickstart documentation: The Firebase library must be initialized once with an Android Create And update name parameter value in your
这篇关于在哪里放Firebase.setAndroidContext()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b 创建 MyApplication.java <
$ b $ pre $ public $ MyApplication extends android.app.Application {
@Override
public void onCreate(){
super.onCreate();
//先前版本的Firebase
Firebase.setAndroidContext(this);
$ b $ // Firebase的新版本
if(!FirebaseApp.getApps(this).isEmpty()){
FirebaseDatabase.getInstance()。setPersistenceEnabled(true);
$ b $ / code $ / $ p
AndroidManifest.xml
:
< application
android:label =@ string / app_name
android:name =。MyApplication>
...
< / application>
I have one RegisterActivity
, and one MainActivity
.
My current flow is - start with MainActivity
- check if user is registered, if not, call RegisterActivity
- after registeration call MainActivity
.Firebase.setAndroidContext()
call.
I have 2 questions:
Application
or once in each Activty
?
Context
. This must happen before any Firebase reference is created or used.MyApplication.java
:public class MyApplication extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
//Previous versions of Firebase
Firebase.setAndroidContext(this);
//Newer version of Firebase
if(!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}
}
AndroidManifest.xml
:<application
android:label="@string/app_name"
android:name=".MyApplication">
...
</application>