Hi folks,
I'm working with PreferenceActivity/PrefrenceFragment for the first
time, and I'm trying to do something that may be a bit tricky. I have
two related items in my preference XML, one of which is disabled by
default. The other item is meant to (and does successfully courtesy of
OnPreferenceTreeClick()) trigger in-app billing which (on successful
purchase) enables (unlocks) the first menu item and disables (locks) the
second so that the item can't be purchased again.
In-app billing issues aside, I need to know how to enable and
disable preference items in such a way that it persists between
application launches. Attempting to achieve this, I added the following
code to my PreferenceFragment.onCreate():
addPreferencesFromResource(R.xml.preferences);
Preference unlocker = this.findPreference("unlock_item");
unlocker.setOnPreferenceClickListener(this);
SharedPreferences prefs =
this.getActivity().getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
if( prefs.getBoolean("feature_locked", true) == false)
{
unlocker.setEnabled(false);
Preference featurePref =
this.findPreference("feature_enabled");
featurePref.setEnabled(true);
}
I'm using the PreferenceFragment as the onPreferenceClickListener:
public boolean onPreferenceClick(Preference preference) {
boolean handled = false;
if( preference.getKey().equals("feature_locked"))
{
handled = true;
Preference featurePref =
this.findPreference("feature_enabled");
featurePref.setEnabled(true);
preference.setEnabled(false);
SharedPreferences pref = preference.getSharedPreferences();
Editor e = pref.edit();
e.putBoolean("feature_locked", false);
e.putBoolean("feature_enabled", true);
e.commit();
//trigger in-app billing
}
return handled;
}
For development purposes, I'm attempting to do the correct
enabling/disabling at the time the preference item is clicked, and I'm
using one of the test codes for in-app billing to approve it. The
problem is that when I get back from the in-app billing, the unlocker is
still enabled and the feature preference is still disabled. Even though
I set the preferences as well, everything is as defined in the
preference XML, with the feature disabled and the unlocker enabled.
I've been hard at work over the last few days getting the in-app
billing working, so please forgive me if I'm missing something simple or
just doing something wrong. Is there some way to get this to work the
way I envision, or should I start over from scratch?
Thank you,
Raymond
P.S. In case it matters, I'm doing my testing on a stock Nexus One
(2.3.6), rooted but stock Huawei Ascend (2.2), and stock Galaxy Nexus
(4.0.4), and getting the same results across the board.
--
Raymond Rodgers
http://www.badlucksoft.com/
http://anevilgeni.us/
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en