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