Where to put Firebase.setAndroidContext() function

后端 未结 6 1450
长发绾君心
长发绾君心 2020-12-20 15:57

I\'m trying to play a bit with Firebase and Android.
I have one RegisterActivity, and one MainActivity. My current flow is - start with M

相关标签:
6条回答
  • 2020-12-20 16:19

    To quote (step 4 of) the Firebase quickstart documentation:

    The Firebase library must be initialized once with an Android Context. This must happen before any Firebase reference is created or used.

    Create 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);
            }
        }
    }
    

    And update name parameter value in your AndroidManifest.xml:

    <application 
            android:label="@string/app_name"
            android:name=".MyApplication">
    ...
    </application>
    
    0 讨论(0)
  • 2020-12-20 16:26

    In the new SDK, it's no longer necessary to call Firebase.setAndroidContext() so you can remove it from your code.

    You can read about updates and changes here: https://firebase.google.com/support/guides/firebase-android

    0 讨论(0)
  • 2020-12-20 16:28

    As seen in the sample applications of Firebase you should place it inside your Application.

    package com.firebase.androidchat;
    
    import com.firebase.client.Firebase;
    
    /**
     * @author Jenny Tong (mimming)
     * @since 12/5/14
     *
     * Initialize Firebase with the application context. This must happen before the client is used.
     */
    public class ChatApplication extends android.app.Application {
        @Override
        public void onCreate() {
            super.onCreate();
            Firebase.setAndroidContext(this);
        }
    }
    

    Source


    Firebase.getAndroidContext()

    After setting the Application context once you can use it where ever you need it. You can retrieve it as often as you like, everywhere. I would also recommend to use the Firebase.getAndroidContext() instead of storing it into variables to prevent MemoryLeaks

    0 讨论(0)
  • 2020-12-20 16:32

    I do not know FireBase but i know Android.. A Context is a global information about an application environment. Your Activity is a Context so i am pretty sure Firebase.getAndroidContext() retrieves your Application Context which is getApplicationContext(), Since that seems sensible.

    Should I only call this function once in Application or once in each Activty?

    call it whenever you need a Context with respects to FireBase codes- but i think will suit best if you call it in your Application class

    If the answer for question 1 is only once, then where should I put it ?

    what if its not once? where do you call it? i guess you will call it anywhere you need Context right? so do that irrespective of question 1's answer, but you can fall on Class.this , getBaseContext() or View.getContext() anytime

    0 讨论(0)
  • 2020-12-20 16:32

    You can do both. If you set it just once then it should be here. Anywhere else and your app will crash. Debugger will say that you have not setAndroidContext():

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);
    // The rest of your code goes here
    

    If you want to use for multiple activities then create a class that has the same name as the app, make sure that the class extends Application.

    public class NameOfApp extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        Firebase.setAndroidContext(this);
    }
    

    }

    After that update the application tag in the manifest.xml file with the following:

    android:name=".NameOfApp"
    
    0 讨论(0)
  • 2020-12-20 16:36

    Replace Firebase.setAndroidContext(this); code with mRef = FirebaseDatabase.getInstance().getReference();

    And add private DatabaseReference mRef; before onCreate methode.

    I hope this will work for Android Studio 3.5 version.

    0 讨论(0)
提交回复
热议问题