0

In my project when a user presses on a button it sets the variable clicked = true and changes TextView colour to orange, the variable clicked is stored to a database in Firebase Firestore and fetched when activity is started. When I close the app and run it, the variable clicked is returned as true but it does not apply to my ifstatement and returns the TextView colour to default.

 @Override
    protected void onStart(){
        super.onStart();
        DocumentReference documentReference = ffstore.collection("user_progress").document(userId);
        documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                if (userAuth.getCurrentUser() != null){
                    if (documentSnapshot.getBoolean("introOption_1_clicked") != null) {
                        clicked = (documentSnapshot.getBoolean("introOption_1_clicked"));
                        Log.i("Message", "Clicked is true "+  clicked);
                    } else {

                    }
                } else {

                }

            }
        });

        if (introOption1Class.returnintroFinishOp1Btn() == true) {
            progressTxt.setText("Finished");
            progressTxt.setTextColor(Color.parseColor("#20df9a"));
            Log.i("MessageTF", "Op1");
        } else if (clicked == true) {
            progressTxt.setText("Started");
            progressTxt.setTextColor(Color.parseColor("#ffb000"));
            Log.i("MessageTF", "Op2");
        } else if (introOption2Class.returnintroFinishOp2Btn() == true && clicked == true) {
            progressTxt.setText("Finished");
            progressTxt.setTextColor(Color.parseColor("#20df9a"));
            Log.i("MessageTF", "Op3");
        }

    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
McJoggin
  • 59
  • 5
  • what does your document look like ? – Lena Bru Feb 20 '20 at 22:15
  • @LenaBru https://prnt.sc/r5e7be – McJoggin Feb 20 '20 at 22:23
  • 1
    your code that acts upon the mutable `clicked` `boolean` variable needs to react to its value/state in the `EventListener::onEvent` callback. Currenly the variable will always be `false` when the code runs. – Mark Feb 20 '20 at 22:33
  • 1
    it seems to me, that the entry points when You check the variable and setting the colors aren't the same...check [documentation of lifecycles](https://developer.android.com/guide/components/activities/activity-lifecycle) – GGK stands for Ukraine Feb 20 '20 at 22:35
  • 1
    Data is loaded from Firestore asynchronously, and while that is happening your main code path continues running (so that the user can keep using the app). You should be able to see this in your logcat output because `Message ` will appear before `MessageTF `. This means that any code that depends on the data from Firestore needs to be **inside** the `onEvent` method. See my explanation here: https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413 – Frank van Puffelen Feb 20 '20 at 23:09

0 Answers0