1

I have a checkboxpreference which performs a tasks once it is clicked on. I want to uncheck it if the task fails. But I am not sure why it is not working.

The preferences.xml:

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

I set the default value to false (unchecked).

I created an activity that implements change listener:

public class settingsActivity extends AppCompatActivity 
implements SharedPreferences.OnSharedPreferenceChangeListener {
....
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new settingsFragment())
                .commit();


        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        settings = PreferenceManager.getDefaultSharedPreferences(this);

        settings.registerOnSharedPreferenceChangeListener(this);
        settingPlayBilling();

        }
}
@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Toast.makeText(settingsActivity.this, "onSharedPreferenceChanged", Toast.LENGTH_SHORT).show();
        mIabHelper.launchPurchaseFlow(settingsActivity.this, ITEM_SKU, 10001, mIabPurchaseFinishedListener, "mypurchasetoken");

    }

However, when checking if in-app purchase failed:

    mIabPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        @Override
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if(result.isFailure()){
                premium = false;                                         
                settings.getBoolean("premium_support", false);

                return ;
            }
            else if(purchase.getSku().equals(ITEM_SKU)) {
                premium = true;
                settings.getBoolean("premium_support", true);
                consumeItem();

            }
        }
    };

the problem is that even if the in-app purchase fails, the checkbox is marked. The checkbox changes based on the number of clicks.

Any help will be appreciated

Rain Man
  • 1,163
  • 2
  • 16
  • 49

1 Answers1

0

You are trying to read a sharedPreference value when you do : settings.getBoolean("premium_support", false);

To save a value in you sharedpreference, you should do : settings.edit.putBoolean("premium_support", false).apply();

Hope this will help.