Where to put Firebase.setAndroidContext() function

Related searches

I'm trying to play a bit with Firebase and Android. 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.

I'm having trouble with where to put the Firebase.setAndroidContext() call.
I have 2 questions:

  1. Should I only call this function once in Application or once in each Activty?
  2. If the answer for question 1 is only once, then where should I put it ?

Thank you all, Giora.


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>

Where to put Firebase.setAndroidContext() function, I'm having trouble with where to put the Firebase.setAndroidContext() call. I have 2 questions: Should I only call this function once in Application or once in each  I'm having trouble with where to put the Firebase.setAndroidContext() call. I have 2 questions: Should I only call this function once in Application or once in each Activty? If the answer for question 1 is only once, then where should I put it ? Thank you all, Giora.


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

Where to put Firebase.setAndroidContext() function, Where to put Firebase.setAndroidContext() function. Question. I'm trying to play a bit with Firebase and Android. I have one RegisterActivity , and one  Introduction. This blog post is about android firebase authentication tutorial using firebaseui. In a web and app world, user authentication is gradually becoming more and more complex in its server side design and implementation.


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

Java Code Examples com.firebase.client.Firebase.setAndroidContext, Firebase.setAndroidContext. The examples are extracted from open source Java for showing how to use setAndroidContext() of the com.firebase.client. onCreate(savedInstanceState); //Necessary to set Firebase context before using it. How to configure Firebase project first time and send multiple data into Firebase real time database from android application. Firebase is a famous product from Google. It comes with many different facilities like Firebase Analytics, Firebase Real Time database, Firebase Cloud messaging, Firebase Storage, Firebase Static website hosting etc.


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

Java Code Examples com.firebase.client.Firebase, This page provides Java code examples for com.firebase.client. toString(​longitude)); map.put("Distrito", district); Map<String, Object> setAndroidContext​(this); mFirebase=new Firebase(FIREBASE_URL); mFirebase.child(​FIREBASE_ROOT_NODE). addListenerForSingleValueEvent(new ValueEventListenerAdapter()  Each time the data at the given Firebase location changes, this method will be called for each item that needs to be displayed. The arguments correspond to the mLayout and mModelClass given to the constructor of this class. Your implementation should populate the view using the data contained in the model.


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"

Android-Firebase-Custom-Auth-Java/MainActivity.java at master , An android project which implements all java libraries required for Firebase auth with custom token Firebase.setAndroidContext(this);. // set your Firebase link. // edit strings.xml to set your Firebase link getMessage()+"\n\nHint: Did you set your Firebase link and secret in strings.xml (You may comment this function out​). I want to append some data in firebase, but it's getting replaced instead. I start with: scorching-fire-2211 Latlng: "7.00003" REGISTER_NO: "13MSE0149" Now when I try to add new REGISTER_NO


Where to put Firebase.setAndroidContext() function. android,firebase,firebase-android. 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.


To get started with Cloud Functions, try working through this tutorial, which starts with the required setup tasks and works through creating and deploying two related functions: addMessage (), which exposes a URL that accepts a text value and writes it to the Realtime Database. makeUppercase (), which triggers on the Realtime Database write


The developer deploys the function, and Firebase connects it to the selected event provider. When the event provider generates an event that matches the function's conditions, the code is invoked. If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.