How to check if a Firebase App is already initialized on Android
Solution 1
On firebase web, you check if already initialized with:
if (firebase.apps.length === 0) {
firebase.initializeApp({});
}
Solution 2
In v9, Firebase has been modularized for better tree shaking. So we can no longer import entire app and check the apps
property AFAIK. The below approach can be used instead.
import { initializeApp, getApps, getApp } from "firebase/app";
getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();
https://firebase.google.com/docs/reference/js/v9/app.md#getapps for documentation
Solution 3
For those wondering how to do the same as the accepted answer, in Android:
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseApp.initializeApp(context);
}
and in an instrumented test environment, use this context:
InstrumentationRegistry.getContext()
Solution 4
Firebase Version 9
import { initializeApp, getApp } from "firebase/app";
const createFirebaseApp = (config = {}) => {
try {
return getApp();
} catch () {
return initializeApp(config);
}
};
const firebaseApp = createFirebaseApp({/* your config */})
Solution 5
You can try to get the Firebase app instance, in it's code firebase checks if it's initialized, if not it throws an IllegalStateException
.
try{
FirebaseApp.getInstance();
}
catch (IllegalStateException e)
{
//Firebase not initialized automatically, do it manually
FirebaseApp.initializeApp(this);
}
Relm
Updated on July 27, 2021Comments
-
Relm almost 3 years
With the following, the first time it's called it works, but then fails on subsequent calls with "FirebaseApp name [DEFAULT] already exists!"
public FirebaseDatabase conn(Context c) { FirebaseOptions options = new FirebaseOptions.Builder() .setApiKey("key") .setDatabaseUrl("url") .setApplicationId("ID") .build(); /////I tried Try and Catch with no success////// FirebaseApp app = FirebaseApp.initializeApp(c, options); /// for this : FirebaseApp app = FirebaseApp.initializeApp(c, options, "some_app"); //// will fail with "FirebaseApp name some_app already exists!" return FirebaseDatabase.getInstance(app); }
All of the above is an attempt to connect to a second Firebase App.
-
Vlad over 6 yearsCool but in android this impossible
-
Anand Rockzz almost 5 yearsdoesn't work anymore - github.com/zeit/next.js/issues/1999#issuecomment-302244429
-
eoinmullan almost 4 yearsI had been using the
App
returned frominitializeApp()
but with the above approach this is no longer possible, so I'm using nowfirebase.app('[DEFAULT]')
, but I'm not sure if that's recommended. -
Pumuckelo almost 3 years@eoinmullan this worked for me githubmemory.com/repo/firebase/firebase-js-sdk/issues/5037
-
cjmling over 2 yearsand in case if you want to get instance in else you can get by
firebase.app();
-
Ayush Kumar over 2 yearsCan you update it to web 9?
-
Levi Wilkerson about 2 yearsThank you. One of the few explanations that make sense for being able to export the firebaseApp variable.
-
user3399180 about 2 yearsI tried this but I get this error - stackoverflow.com/questions/72028182/…