Anthea
Anthea

Reputation: 3809

Android load SharedPreferences for widget into Preference Activity

i am able to link an shared preference to an activity that way:

 Intent intent = getIntent();
 Bundle extras = intent.getExtras();
 if (extras != null) appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                                 AppWidgetManager.INVALID_APPWIDGET_ID);

 if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) finish();

 SharedPreferences pref = getSharedPreferences("Widget"+appWidgetId, 0);

In the configuration dialog of my preference application I can access the displayed preferences via:

 getPreferenceScreen().getSharedPreferences();

These preferences are not linked to a widget but global. How can I change the preferences that are displayed in my preference activity to the widget aligned preferences?

I tried to set the shared preference file manually:

  PreferenceManager localPrefs = getPreferenceManager();
  localPrefs.setSharedPreferencesName("GITC_Prefs"); 

This leads to a null pointer exception when I try to access the shared Preferences via

 PreferenceActivity.getPreferenceScreen();

Upvotes: 0

Views: 1244

Answers (1)

a.ch.
a.ch.

Reputation: 8390

To access same SharedPreferences in two activities you need to specify same name for PreferencesActivity.getPreferenceManager().setSharedPreferencesName(name) and Activity.getSharedPreferences(name, 0). You currently specify "Widget" + appWidgetId in the first case and "GITC_Prefs" in the second.

Upvotes: 3

Related Questions