0

In my code, when user click on a button, it calls a function.In that function data has to loaded from firebase. On successful of loading, Intent will pass to another activity with some putExtra data.

SharedPreferences pref = getApplicationContext().getSharedPreferences("My_pref", MODE_PRIVATE);
        choice = pref.getInt("language", 0);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               if(choice==1)
                {
                    firebase("param1","param2","English");
                }
                if(choice==2)
                {
                    firebase("param1","param2","hindi");
                }

            }
        });

 public void firebase(String files,String titles,String language)
    {

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference(language);
        DatabaseReference myRef1=myRef.child(titles);
        DatabaseReference myRef2=myRef.child(files);
        myRef1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                language_title = dataSnapshot.getValue(String.class);
                t=1;

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        myRef2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                language_file= dataSnapshot.getValue(String.class);
                f=1;
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

            if(t==1&&f==1) {
                Intent i = new Intent(getApplicationContext(), caf.class);
                i.putExtra("titles", language_title);
                i.putExtra("files", language_file);
                startActivity(i);
            }

    }

But in that case intent is only passing when I click twice.In single click Intent is not passing. where is the problem?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
suraj pal
  • 29
  • 5

1 Answers1

0

Any code that needs data from the database, needs to be inside the onDataChange method or be called from there.

See my answer here for a longer explanation: getContactsFromFirebase() method return an empty list.

Since you have two onDataChange methods, you'll need to make sure both have been called before starting the new activity. Using some simple flag variables to track state, you can do that with:

Declare two member fields:

boolean isTloaded, isFLoaded;

And then

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference(language);
    DatabaseReference myRef1=myRef.child(titles);
    DatabaseReference myRef2=myRef.child(files);

    isTloaded = false;
    isFloaded = false;

    myRef1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            language_title = dataSnapshot.getValue(String.class);
            t=1;
            isTloaded = true;
            if (isTloaded && isFloaded) {
                startCafActivity();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException(); // never ignore errors
        }
    });
    myRef2.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            language_file= dataSnapshot.getValue(String.class);
            f=1;
            isFloaded = true;
            if (isTloaded && isFloaded) {
                startCafActivity();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException(); // never ignore errors
        }
    });

And then finally:

private void startCafActivity() {
    if(t==1&&f==1) {
        Intent i = new Intent(getApplicationContext(), caf.class);
        i.putExtra("titles", language_title);
        i.putExtra("files", language_file);
        startActivity(i);
    }
}

It might be even simpler, but I had a hard time figuring out your application logic due to the variable names. Consider using more descriptive named than t, f and caf to make it easier for others (and your future self) to understand what each part of the code does in isolation.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807