You only need the XML file containing the PreferenceScreen if you want
to show an activity that is similar to the Android settings screen.
You'd have to subclass PreferenceActivity in that case and call
addPreferencesFromResource() in onCreate().
http://code.google.com/android/reference/android/preference/PreferenceActivity.html#addPreferencesFromResource(int)

If you provide your own UI to change preferences, such as your two
buttons to read and write, you can remove the XML.

The rest of the code looks okay and should work fine.

Christoph


On Tue, Jan 27, 2009 at 4:35 AM, radiolistener
<radioliste...@optonline.net> wrote:
>
> My issue was simpler than that.  I was trying to get any setting to
> read, write, save, then recall under any condition.  What I was doing
> wrong was I didn't have the xml file set up right.  It appears to work
> fine now.
>
> Thanks, it was from studying your code that I figured out what was
> wrong.  So far, it works with power down, and a re-install, does it
> also work with upgrades?
>
> Here's the source code:
>
> package ree.sharedpreferences.share;
>
> import android.app.Activity;
> import android.content.SharedPreferences;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.Button;
> import android.widget.TextView;
>
> public class actor extends Activity {
>    /** Called when the activity is first created. */
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        setContentView(R.layout.main );
>        Button setter = (Button) findViewById(R.id.setter );
>        Button readinger = (Button) findViewById(R.id.readinger );
>
>        setter.setOnClickListener(new View.OnClickListener() {
>
>                        public void onClick(View view) {
>
>                                SharedPreferences preferences = 
> getSharedPreferences
> ("first_preferencescreen" , MODE_PRIVATE);
>
>                                SharedPreferences.Editor editor = 
> preferences.edit();
>
>                                editor.putString("eyesight", "Radar"); // 
> value to store
>
>                                editor.commit();
>
>
>                        }
>
>                });
>
>        readinger.setOnClickListener(new View.OnClickListener() {
>
>                        public void onClick(View view) {
>
>                                SharedPreferences preferences = 
> getSharedPreferences
> ("first_preferencescreen" , MODE_PRIVATE);
>                                String j = preferences.getString("eyesight", 
> "radio");
>
>                                 TextView k = (TextView) 
> findViewById(R.id.lets_read  );
>                             k.setText(j);
>
>                        }
>
>                });
>
>
>
>    }
> }
>
> /*<Button android:id="@+id/setter"
>        android:text="SetSetting"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:textSize="22px"
> />
> <Button android:id="@+id/readinger"
>        android:text="ReadSetting"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:textSize="22px"
>  />
> */
>
> And here's the xml, which is almost verbatim yours:
>
>
> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
> android" android:key="first_preferencescreen">
>
> <PreferenceCategory android:title="PrefCategory" android:order="1">
>                <EditTextPreference android:key="eyesight"
>                        android:title="eyes"
>                        android:summary="degree of resolution"
>                        android:singleLine="true"
>                        android:dialogMessage="Resolution"
>                        android:persistent="true"/>
>
> </PreferenceCategory>
> </PreferenceScreen>
>
>
>
> On Jan 26, 9:09 pm, Christoph Studer <chstu...@gmail.com> wrote:
>> If you do not store values with special characters in your settings,
>> it might be interesting to see some of your code in order to analyze
>> why you're losing them.
>>
>> If we're only talking about the problem with special characters, you
>> should be fine with Base64 encoding values that potentially lead to
>> problems. I think this puts the least overhead on the device and on
>> you as the developer. A simple wrapper class around these values that
>> encodes and decodes them in front of the SharedPreferences would be
>> enough.
>>
>> Especially putting it onto the SD card adds a lot of special cases
>> that you'd have to handle yourself, e.g. SD card not mounted or being
>> unmounted (which will kill your process if you don't handle it
>> correctly).
>>
>> Christoph
>>
>> On Tue, Jan 27, 2009 at 2:54 AM, radiolistener
>>
>> <radioliste...@optonline.net> wrote:
>>
>> > I've been struggling with these shared preferences.  I want to save a
>> > resolution setting, so that people with different degrees of vision
>> > can see the screen.  With a Diabetes application, this is a very
>> > important feature.
>>
>> > What I've found thus far, however I cut the cake, I lose my settings
>> > with either a cold boot or a re-install.  This isn't readily tenable.
>> > So, I'm thinking of either writing them to the database or writing
>> > them to the sdcard.  I'm weighing those two options now.
>>
>> > Any insights would be appreciated.
>>
>> > Thanks,
>>
>> > John aka Radiolistener
>>
>> > On Jan 25, 5:03 pm, Christoph Studer <chstu...@gmail.com> wrote:
>> >> Yeah, Base64 encoding would certainly help until the platform bug is 
>> >> fixed.
>>
>> >> I guess a lot of people with strong passwords (i.e. passwords
>> >> containing special characters) are in trouble with a lot of apps right
>> >> now.
>>
>> >> It's funny you mention two apps I share parts of the codebase with. I
>> >> stripped down ImapStore for my app and included it as a jar in my
>> >> package. But it's very very unlikely that this would have an impact.
>>
>> >> Christoph
>>
>> >> On Sun, Jan 25, 2009 at 5:53 PM, Daniel <android-...@danapple.com> wrote:
>>
>> >> > This has been a recurring problem with K-9:
>> >> >http://code.google.com/p/k9mail/issues/detail?id=143
>> >> > and apparently the core Email as well:
>> >> >http://www.androidforums.com/showthread.php?p=16842
>> >> > and possibly other apps, too:
>> >> >http://groups.google.com/group/android-developers/browse_thread/threa...
>> >> >http://www.mail-archive.com/android-developers@googlegroups.com/msg13...
>>
>> >> > Your observation about the < and > makes sense to me, as one reason
>> >> > the preferences can fail to save or load is due to XML generation or
>> >> > parsing problems.  I've thought that perhaps Base64 encoding all of
>> >> > the preference values might be a smart move.
>>
>> >> > Dan.
>>
>> >> > On Jan 24, 11:56 am, Christoph Studer <chstu...@gmail.com> wrote:
>> >> >> Hello,
>>
>> >> >> I'm making use of SharedPreferences in my application. One part is a
>> >> >> PreferenceActivity and the other part is a backend that accesses the
>> >> >> preferences using PreferenceManager.getDefaultSharedPreferences(...).
>> >> >> Both parts are running in the same process, but potentially in
>> >> >> different threads.
>>
>> >> >> Now, from time to time, I encounter a reset of all the preferences. It
>> >> >> looks to me like this happens when my process is killed (e.g. when
>> >> >> installing a new version or starting the activity or service the first
>> >> >> time after a long time). However, it is not reproducible by killing
>> >> >> the process manually and it does not happen consistently.
>>
>> >> >> Are there other people who have the same problem? Is there a race
>> >> >> condition that could lead to something like this?
>>
>> >> >> The source code of my app can be found 
>> >> >> here:http://code.google.com/p/android-sms/source/browse/
>>
>> >> >> The relevant files 
>> >> >> are:http://code.google.com/p/android-sms/source/browse/trunk/android-clie...
>> >> >> andhttp://code.google.com/p/android-sms/source/browse/trunk/android-clie...
>>
>> >> >> Thank you,
>>
>> >> >> Christoph
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to