0

Hi I have a boolean return type method in my android app. How ever in my method I am calling an api but my method immidately returns the first boolean value without waiting for the api callback part of the code.. how do i solve this? please take a look at the following code:

    public boolean validateForm() {
            flag=true;
    //---- THIS CODE DOES NOT EXECUTE.. THE RETURN STATEMENT IS DIRECTLY EXECUTED---
                checkSourceCode(new BooleanCallBack() {
                    @Override
                    public void onSuccess(boolean result) {
                        validateCode = result;
                        if (!validateSourceCode(result)) {
                            flag = false;
                        } 
                        if (flag) {
                            saveData();
                        }
                    }
                }); 
   //---- THE ABOVE CODE DOES NOT EXECUTE.. THE RETURN STATEMENT BELOW IS DIRECTLY EXECUTED

                return flag; //This part is directly RETURNED without waiting for the above part of the code to execute
        }

If i follow the answer given by Juan Felippo I have another issue to deal with then:

This is where I am calling the validateForm() method:

   public boolean validate(int position) {

        switch (position) {
            case 0:
                if (signUp_bgp_1.validateForm()) {
                    signUp_bgp_2.updateFields();
                    return true;
                } else {
                    return false;
                }

            case 1:
               signUp_bgp_2.validateForm(new BooleanCallBack() {
                   @Override
                   public void onSuccess(boolean result) {
                       return result; // I GET ERROR HERE "CANNOT RETURN A VALUE FROM A VOID RETURN TYPE"
                   }
               });
            case 2:
                return signUp_bgp_3.validateForm();

HERE IS MY COMPLETE CODE:

*I call validateForm method on case 1

  public boolean validate(int position) {
            switch (position) {
                case 0:
                    if (signUp_bgp_1.validateForm()) {
                        signUp_bgp_2.updateFields();
                        return true;
                    } else {
                        return false;
                    }

                case 1:
                    signUp_bgp_2.validateForm(new BooleanCallBack() {
                        @Override
                        public void onSuccess(boolean result) {
                            bool =result; //CANNOT RETURN BOOLEAN HERE HOW TO HANDLE THIS?
                        }
                    });
                case 2:
                    return signUp_bgp_3.validateForm();
                case 3:
                    if (signUp_bgp_4.validateForm()) {
                        signUp_bgp_5.updateDetails();
                        return true;
                    } else {
                        return false;
                    }
                case 4:
                    if (signUp_bgp_5.validateForm()) {
//                        signUp_bgp_5.updateDetails();
                        return true;
                    } else {
                        return false;
                    }

                default:
                    return signUp_bgp_1.validateForm();
            }
        }

*My modified validateForm method according to Juan Fellipo's answer

 public void validateForm(final BooleanCallBack mainCallback) {
        flag=true;

            checkSourceCode(new BooleanCallBack() {
                @Override
                public void onSuccess(boolean result) {
                    validateCode = result;
                    if (!validateSourceCode(result)) {
                        flag = false;
                    }

                    if (!accept_checkbox.isChecked()) {
                        Toast.makeText(getActivity().getBaseContext(), "Accept The Terms & Conditions To Proceed", Toast.LENGTH_LONG).show();
                        //        accept_checkbox.requestFocus();
                        flag = false;
                    }

                    if (!validateAddress()) {
                        flag = false;
                    }

                    if (!validateCountry()) {
                        flag = false;
                    }
                    if (!validatelandmark()) {
                        flag = false;
                    }
                    if (!validateDistrict()) {
                        flag = false;
                    }

//        if (!validateCity()) {
//            flag = false;
//        }

//        if (!validateMobilenumber()) {
//            flag = false;
//        }

                    if (!validatePincode()) {
                        flag = false;
                    }


                    if (!validateFullfillment()) {
                        flag = false;
                    }
                    if (flag) {
                        saveData();
                    }
                    mainCallback.onSuccess(flag);
                }
            });
    }
Shahid Sarwar
  • 1,209
  • 14
  • 29
  • Did you use breakpoints to check it your code is entering the onSuccess and the if conditions? It might be that it's jumping right away to return statement – FabioR Nov 01 '16 at 12:59
  • Exactly! it is not waiting for the onSuccess part to execute for some reason I am debugging it as we speak.. – Shahid Sarwar Nov 01 '16 at 13:00
  • It might be that the callback is not being successful, so it's skipping it. Have you tried using an onFailure method? Perhaps try using it and checking it the code is passing through it, this way, you will not only check your code logic, but also if your callbacks are working. – FabioR Nov 01 '16 at 13:04

1 Answers1

3

The api method is asynchronous while your method is synchronous. You can provide a callback when response is ready. Try:

public void validateForm(final BooleanCallBack mainCallback) {
        flag=true;

            checkSourceCode(new BooleanCallBack() {
                @Override
                public void onSuccess(boolean result) {
                    validateCode = result;
                    if (!validateSourceCode(result)) {
                        flag = false;
                    } 
                    if (flag) {
                        saveData();
                    }
                    mainCallback.onSuccess(flag)
                }
            }); 
    }

and call the method:

someObject.validateForm(new BooleanCallBack() {
                @Override
                public void onSuccess(boolean result) {
                    //DO STH WITH RESULT
                }
            });

More info about sync/async and callbacks see : What is a callback in java

Community
  • 1
  • 1
Juan Felippo
  • 224
  • 2
  • 9
  • 1
    you are in the same dilemma here the method: public boolean validate(int position) is a blocking Synchronous call, while the updated method public void validateForm(final BooleanCallBack mainCallback) is Async – Juan Felippo Nov 01 '16 at 13:25