Where to put Firebase.setAndroidContext() function

后端 未结 6 1483
长发绾君心
长发绾君心 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: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

提交回复
热议问题