samedi 28 mai 2016

toggle the value of checkboxpreference in Android

I have a checkboxpreference which performs a tasks once it is clicked on. By default, the checkbox is not checked. After successful completion of the task, it is checked.

content of settings.xml:

<CheckBoxPreference
    android:key="@string/premium_support"
    android:title="Premium Support"
    android:summary="Purchase premium support"
    android:defaultValue="false" />

and my implementation so far:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.settings);

    final CheckBoxPreference checkboxPref = (CheckBoxPreference)
    getPreferenceManager().findPreference("premium_support");
    boolean result; // boolean value to store final result

    // set up a listener for checkbox
    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            boolean toggle = false;
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                // just for testing to show value of checkbox
                Toast.makeText(getActivity(), "Pref changed to " + newValue.toString(), Toast.LENGTH_LONG).show();

                // if the checkbox is marked
                if (newValue.toString()== "true"){
                    // purchase the product
                    bp.purchase(getActivity(),PRODUCT_ID);
                }
                // if purchase was successful 
                if (bp.isPurchased(PRODUCT_ID)){
                    // display item purchased for testing
                    showToast("Item purchased");

                    // set the checkbox as checked
                    checkboxPref.setChecked(true);

                    // also make purchased boolean to true
                    result = true;
                    showToast("value of purchased is: " + result);

                }else{
                    // otherwise the item was not purchased
                    showToast("Item was not purchased");

                    // uncheck the checkbox 
                    checkboxPref.setChecked(false);

                    // set the result to false
                    result  = false;
                    showToast("value of purchased is: " + result);
                    //toggle = false;
                }
                // also check if it was purchased before
                if (result != true){
                    showToast("Item was not previously purchased");
                    checkboxPref.setChecked(false);
                }
                return toggle;
            }
        });

Everything works as expected. But if the purchase is successful, the checkboxpreference does not change unless clicked on it again.

So the user clicks on the checkbox, it brings the billing box (billing uses Android In-App Billing Library), the user purchase and closes but the checkbox is not marked. The user must click on the checkbox again.

Any idea why this is occurring?




Aucun commentaire:

Enregistrer un commentaire