PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

getPreferences() reference to class name used by getSharedPreferences()

I'm trying to use preferences from a Dialog, and have been having some difficulty. The rest of my application just uses the getPreferences() dialog, and doesn't seem to have any problems at all. However, a Dialog can't call a getPreferences(), and for some reason I don't have access to the base Activity. But I've come to a solution, from the SDK documentation, but I need one quick answer to have it.

The SDK states that "This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.". If I can get the activity's class name. I have access to context, which includes getApplicationInfo(). There are a variety of names here. My question is, is one of the names that comes from getApplicationInfo() the same as the value that getPreferences() passes to getSharedPreferences() as the class name?

Upvotes: 2

Views: 937

Answers (2)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

Even better, and this is now the way I use them, is to use this:

final SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(mContext);

This gets the right preferences, without needing to do anything complicated.

Upvotes: 0

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

What I ended up doing was using this as the application name across the board:

getBaseContext().getApplicationInfo().packageName

The context is almost universally available, and this name should be consistent for all classes in the program.

Upvotes: 1

Related Questions