user5492192
user5492192

Reputation: 23

Getting SharedPreferences in Non-Activity class

I have the following class:

public class UserPref {
    Context context;

    SharedPreferences sharedPref;

    public SessionUtils(Context context) {
        this.context = context;
        sharedPref = context.getPreferences(Context.MODE_PRIVATE);
    }
}

However, I get the following error on getPreferences():

Cannot resolve method 'getPreferences(int)'

Why? How do I fix this?

Upvotes: 1

Views: 3217

Answers (1)

M D
M D

Reputation: 47807

It's like

SharedPreferences preferences = context.getSharedPreferences("AppPreferences", Activity.MODE_PRIVATE);

More about it Official Docs

Upvotes: 5

Related Questions