• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Question on Shared Preferences

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello group, I am getting more confused and hopefully someone can help. I am trying to use SharedPreference but I am having no success.

I am new to Android programming but here is a simplification of what I am trying to do. I have one activity, lets call it "ActivityA" and one fragment, lets call it "FragmentA".

I want them to interact in the following way...

step 1. When ActivityA loads up it creates a SharedPref and assigns a value
test_count = 1

step 2. ActivityA calls FragmentA

step 3. FragmentA accesses the SharedPref and depending on the value it will display a different layout. i.e. if test_count is 1 it will use testlayout1, if test_count is 2 it will use testlayout2 etc

step 4. Control goes back up to ActivityA which then reads the value in shared pref,
adds 1 to it and re-saves the value. then things repeat from step #2 above.


I looked at examples online and thought I had at least up to step3 working but I noticed that in step3 FragmentA was not displaying the layout it was supposed to. I soon discovered it was apparently not reading in the value.

So if you could, below is how I am trying to accomplish the above.
First let me get the silly question out the way first.
This SharedPrefs file, do you have to create it yourself manually? My impression was that just by making calls in your code the system would create and modify this file based on calls made in your code, not on anything you manually have to create.

For step 1 above,
I have declared the following as class variables in ActivityA

public static final String MY_PREFS_NAME = "quiz_prefs_file";
SharedPreferences sharedpreferences;

then in the onCreate method of ActivityA I do the following...

sharedpreferences = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("test_count", 3);  //I would start at 1 but this was a test value used
editor.commit();


Step2 works

For Step 3 above
within FragmenA onCreateView method
I have the following

int test_count ;
SharedPreferences mysettings = getActivity().getPreferences(Context.MODE_PRIVATE);

test_count  = mysettings.getInt("test_count ", 0);

This does NOT work. I have used println statements to the console to show that test_count always = 0 (the default value)

For step 4 above
Actually on this part I am not sure so any quick pointers/examples would be appreciated.
 
Harry Wordsworth
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All, thanks for looking, actually it seems a bundle is the way to do what I wanted. I tried that and it works. Still learning this so will make detours every once in a while.
 
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible to save more than one set of preferences.  
In the first code you use a method:
getSharedPreferences( getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); , Context.MODE_PRIVATE);
that uses a named preference:  MY_PREFS_NAME
in the second code:
getActivity().getPreferences(Context.MODE_PRIVATE);
you get the preference for the class which I assume is different from the named one.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic