AndreaF
AndreaF

Reputation: 12395

Android PreferenceManager in staticmethod

If I try to write this method

public static void saveDefaultUser() {
        Editor pName = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext())
                .edit();
        pName.putString("Name", name);
        pName.commit();
}

doesn't compile and I have to remove the static value, because clearly I cannot make a static reference to the non static method.

Is there is a way to adapt this for the use in static method?

Upvotes: 1

Views: 816

Answers (1)

Khantahr
Khantahr

Reputation: 8548

You'll have to pass in a Context to your method and use that instead of getBaseContext().

public static void saveDefaultUser( Context cntxt ) {
    Editor pName = PreferenceManager
            .getDefaultSharedPreferences(cntxt)
            .edit();
    pName.putString("Name", name);
    pName.commit();
}

Upvotes: 4

Related Questions