Where to put Firebase.setAndroidContext() function

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

提交回复
热议问题