How to get access to Firebase realtime database from two different activities?

Published

I am working on a project in which I have created a custom java class in which I have made a firebase database instance so, that when I need access to the database I could make a instance of my custom class and have access to the database and when I call the database through my custom java object in activity "A" it works properly but when I call the database in activity "B" then the activity "B" gets suddenly crashed. How can I resolve this issue?

Custom java class code

'''public class databaseApp  {
    public databaseApp() {
    }

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef =  database.getReference();
    String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
    

    public void writeNewUser(String u, String e, String p, String tcn){


        user user = new user();
        user.setUserName(u);
        user.setEmail(e);
        user.setPhone(p);
        user.setTcn(tcn);
        myRef.child("User").child(getUniqueId()).child("User Data").setValue(user);
       

    }
    public void addBatchToDatabase(String batchName,String startDate,String endDate,String fees, String startTiming,String endTiming){
        addBatchDatabase addbatchDetails = new addBatchDatabase();
        addbatchDetails.setBatchName(batchName);
        addbatchDetails.setStartDate(startDate);
        addbatchDetails.setEndDate(endDate);
        addbatchDetails.setFees(fees);
        addbatchDetails.setStartTimimg(startTiming);
        addbatchDetails.setEndTimimg(endTiming);
               DatabaseReference myref = database.getReference(id);
        myref.push().child("Batch").setValue(addbatchDetails);
    }

'''

Activity "A" code

''' databaseApp databaseApp = new databaseApp();
databaseApp.writeNewUser(N.getText().toString(),E.getText().toString(),P.getText().toString(),tcn.getText().toString());'''

Activity "B" code

 '''
     databaseApp databaseApp = new databaseApp();        databaseApp.addBatchToDatabase(batchName.getText().toString(),startDate.getText().toString(),endDate.getText().toString(),fees.getText().toString(),startTime.getText().toString(),endTime.getText().toString());

'''

Source: Android Questions

Published
Categorized as android, firebase, firebase-realtime-database, java

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq