Ken Reid
Ken Reid

Reputation: 609

Android - Shared Preference with Two Classes

Basically I want to set up one class to have getters and setters to the device to store and retrieve data, and other classes to access it. I managed to get sharedpreferences working in one class but having trouble with two classes (I'm familiar with Java but not Android, I read somewhere I shouldn't be using Activity but static, and couldn't get that working either). Anyway here is the getter / setter class.

public class Storage extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

final SharedPreferences prefs = this.getSharedPreferences(
        "uk.co.kenreid.examplestory", Context.MODE_PRIVATE);

String nameKey = "uk.co.kenreid.examplestory.name";

I access it with these pieces of code in the other class (final is used as the variable "storage" is used within an onclicklistener):

final Storage storage = new Storage();

storage.storeItem("name", name);
System.out.println(storage.getString("name"));

Upvotes: 0

Views: 2457

Answers (7)

user2496783
user2496783

Reputation: 101

You can use a separate class can handle all your preferences

public class PreferenceManager {
    public static void setLogInSuceess(boolean login, Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

        preferences.edit().putBoolean("LOGIN", login).commit();
    }

    public static boolean getLogInSuceess(Context context){
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
        boolean login = preferences.getBoolean("LOGIN", false);

        return login;
    }
}

Use it where ever you want,

PreferenceManager.setLogInSuceess(true/false, context); boolean _login = PreferenceManager.getLogInSuceess(context);

Upvotes: 0

biddulph.r
biddulph.r

Reputation: 5266

I believe that your issue is initialising the SharedPreferences away from the Activity?

Assuming this is your issue, you can pass a Context through the constructor of the Storage class to initalise it.

Eg.

SharedPreferences prefs;

public Storage(Context aContext){
    prefs = aContext.getSharedPreferences("uk.co.kenreid.examplestory", 
            Context.MODE_PRIVATE);
}

Then access via your original acessor methods. Might be best to pass the Application Context rather than Activity Context through to the constructor, so when you create your Storage class, call it like this in the Activity:

Storage myStorage = new Storage(getApplicationContext());

It might be wise to create a Singleton class as well, to allow you to use the same instance of Storage. There are plenty of tutorials for Singletons in Java around the web.

Upvotes: 1

Dino Rino
Dino Rino

Reputation: 55

Try this:

public class Storage extends Application

Application is a class used to share data from Activities

Upvotes: -1

Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

Storing something in shared preference:

    SharedPreferences prefs = getSharedPreferences("TOT", Context.MODE_PRIVATE);  
    Editor ed = prefs.edit();  
    ed.putString(key, value);  
    ed.commit(); 

In your case you could use the above code inside the onCreate method.

Fetching something from shared preference:

    SharedPreferences prefs = getSharedPreferences("TOT", Context.MODE_PRIVATE);
    prefs.getString(key, null);

Upvotes: 1

stinepike
stinepike

Reputation: 54722

check the doc of getSharedPreferences

Here the first parameter is the namem of the Shared Preference where you are storing the data. So you need to use the same name from every classes to access the same shared preferences.

Upvotes: 0

XorOrNor
XorOrNor

Reputation: 8978

try this:

SharedPreferences prefs = this.getSharedPreferences("uk.co.kenreid.examplestory", Context.MODE_MULTI_PROCESS);

Upvotes: 1

schabluk
schabluk

Reputation: 708

Try like this:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());

mVariable = sharedPref.getBoolean(SettingsActivity.KEY_TO_VARIABLE, false);

Upvotes: 0

Related Questions