简体   繁体   中英

Android how to wait for onComplete method to end before proceeding to the next code

I want to get a boolean from my onComplete function before proceeding to the next step.

My problem is that when code gets to the part when my boolean (isLoginSucces) is crucial to my code, it get's only false value, because onComplete function did not arrived at the point when it changes the value of the boolean.

I tried async function, but it didn't work, tried aswell AtomicBoolean, didn't worked too.

After that, i did put even Thread.sleep(10000). In that time(10 seconds) it surely changed the value, but don't work too, still false, i don't get it.

    private String loginButtonClicked () {

            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        ...
                        LoginFragment.this.isLoginSucces = true;
                        Log.d("boolean", "boolean value in onComplete: " + isLoginSucces);
                    } else {
                        ...
                        LoginFragment.this.isLoginSucces = false;
                    }
                }
            });

            //HERE I tried Thread.sleep(10000);
            Log.d("boolean", "boolean value after function: " + isLoginSucces);

            if (LoginFragment.this.isLoginSucces)
                return email;
            else
                return null;

    }

The log output is :

2019-03-29 18:45:12 D/boolean: boolean value after function: false

2019-03-29 18:45:14 D/boolean: boolean value in onComplete: true

public void methodThatCallsLoginButtonClicked() {
    // code before calling loginButtonClicked 

    loginButtonClicked();

    // nothing else that needs the value from login button clicked should be here
}

public void afterLogin(boolean result) {
    // any code that needs the value of the login callback
}

private void loginButtonClicked () {

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                afterLogin(task.isSuccessful());
            }
        });

}

Declare

String str_email = ""; 

at the top of your code.

Insert dependent code inside oncomplete method removing the return statement and changing for:

private String loginButtonClicked () {

            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    ...
                    LoginFragment.this.isLoginSucces = true;
                    Log.d("boolean", "boolean value in onComplete: " + isLoginSucces);
                } else {
                    ...
                    LoginFragment.this.isLoginSucces = false;
                }

               if (LoginFragment.this.isLoginSucces)
                   str_email = email;
               else
                   str_email = ";

        });
}

Do your code with

if(!str_email.equals("")){

//if str_email is different from ""


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM