1

After executing an exercise, I wanted to show an alert dialog that says, "You have already executed this. Are you sure you want to do it again?"

So in my firebase, once the exercise has already been executed isProgramExerciseFinished will be set to true. But in my code, it seems that the dialog only shows once all of it is set to true.

public void onClick(View view) {
            currentWeek = programTrackers.get(0).getWeek();
            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ProgramTracker")
                    .child(GlobalUser.getmUser().getiD())
                    .child(programTrackers.get(0).getProgramId());
            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final int childCount = (int) dataSnapshot.getChildrenCount();
                    int count = 0;
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        ProgramTracker programTracker = snapshot.getValue(ProgramTracker.class);
                        if (programTracker.getProgramExerciseWeek() == currentWeek) {
                            programTrackChecker.add(programTracker);


                        }
                        count++;
                    }
                    if (count == childCount) {
                        boolean isFinished = false;
                        //last iteration
                        for (int i = 0; i < programTrackChecker.size(); i++) {
                            if (programTrackChecker.get(i).isProgramExerciseFinished()) {
                                isFinished = true;
                            } else
                                isFinished = false;
                        }
                        if (isFinished) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(DayExerciseActivity.this);
                            builder.setMessage("You have already completed all exercises. Are you sure you want to do it again?");
                            builder.setPositiveButton("Yes", (dialogInterface, i) -> {
                                startActivity(new Intent(DayExerciseActivity.this, DoProgramActivity.class)
                                        .putExtra("programTrackers", programTrackers)
                                        .putExtra("exerciseNum", String.valueOf(0)));

                            });
                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });
                            builder.show();

                        } else
                            startActivity(new Intent(DayExerciseActivity.this, DoProgramActivity.class)
                                    .putExtra("programTrackers", programTrackers)
                                    .putExtra("exerciseNum", String.valueOf(0)));

                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });
}

once it is true a dialog will show once it is false dialog will not be shown and automatically will be directed to doProgramActivity once it is true a dialog will show once it is false dialog will not be shown and automatically will be directed to doProgramActivity

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
asianoobdev
  • 65
  • 1
  • 7

1 Answers1

2

I believe that you are being returned with the data from firebase only once. As the callback function is called asynchronously. So by the time it is invoked, postsRef.on(...) has already returned and any code immediately after it will have run. Refer to the following question: Firebase on() does not return anything

Huzaifa Asif
  • 658
  • 1
  • 8
  • 24