At the start of the app, I want to download the database from Firebase and create array lists that will be used by different activities. From Jeff Gilfelt answer at Android global variable I learned how to make global variable available to all activities. But I can't figure out how to use this to make an arraylist from Firebase Database a global variable. I tried it this way, as described on the other question on making global variable:
public class FirstFirebaseCall extends Application {
private ArrayList<ItemAndPrice> listGroceryAndPrice = new ArrayList<>();
private final FirebaseDatabase database = FirebaseDatabase.getInstance();
private final DatabaseReference dbf = database.getReference("Accounts");
private final FirebaseUser mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
@Override
public void onCreate() {
super.onCreate();
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
ChildEventListener childEventListener = dbf.child(Objects.requireNonNull(mCurrentUser).getUid()).child("Inventory")
.addChildEventListener(new ChildEventListener() {
ItemStockInitialSellingPrice itemInventory = new ItemStockInitialSellingPrice();
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
itemInventory = dataSnapshot.getValue(ItemStockInitialSellingPrice.class);
String item = Objects.requireNonNull(itemInventory).getItem();
double price = itemInventory.getSellingPrice();
ItemAndPrice itemAndPrice = new ItemAndPrice(item, price);
listGroceryAndPrice.add(itemAndPrice);
It did not worked out and displayed the error:
IllegalStateException: Default FirebaseApp is not initialized in this process com.example.storehelper. Make sure to call FirebaseApp.initializeApp(Context) first.
Where should I call FirebaseApp.initializeApp(Context) in my code?