Add several appWidgets with different configuration?

后端 未结 1 1803
深忆病人
深忆病人 2021-01-06 22:35

I\'ve created a widget which displays a simple textview, which is editable as an Edittext in a configuration activity. I save the inputted text with shared preferences, so t

相关标签:
1条回答
  • 2021-01-06 23:00

    You probably don't want to use getDefaultSharedPreferences here. All widgets share the same default shared preferences, so they will be constantly overwriting each other.

    I had the same situation in my own app, so I used a custom preference file for each widget. You can name the preference file with the widgetID, and then each widget will always get it's own unique set of preferences.

    In the configuration PreferenceActivity:

    this.getPreferenceManager().setSharedPreferencesName(
               "widget" + String.valueOf(mAppWidgetId)
    );
    

    This will store all of the PreferenceActivity settings into a preference file named after the widget id.

    Then in the widget itself, just retrieve the file corresponding to its id:

    preferences = context.getSharedPreferences(
          "widget" + String.valueOf(appWidgetId)
          , Context.MODE_PRIVATE);
    

    And retrieve preferences like normal.

    0 讨论(0)
提交回复
热议问题