0

I have problem with PreferenceActivity used to set preferences for app widget. The thing is, that when I want to pass to the PreferenceActivity any info via Intent only at the first time I am able to read them. After when information sended in Intent are different, those earlier sended are still saved and I don't see new one. The only thing to do that is to remove completly app from system and install again. But then again everything will looks this same (first info will stay). The other important thing is that I am not saving it anywhere, just showing it in label text and in LogCat.

Those are code fragments responsible for this:

Widget activity

            // Actions required to open settings activity
            Intent intentSettings = new Intent(context, PrefActivity.class);
            intentSettings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                    appWidgetIds);
            intentSettings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, (allWidgetIds.length+number));
            PendingIntent pendingIntentSettings = PendingIntent.getActivity(
                    context, 0, intentSettings, 0);
            remoteViews.setOnClickPendingIntent(R.id.imb_ustawienia,
                    pendingIntentSettings);

Preference activity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("Info","onCreate");

        addPreferencesFromResource(R.xml.pref);

        Bundle intentIn = getIntent().getExtras();
        Log.d("SIZE", "" + intentIn.size());
        if (intentIn == null) {
            Log.d("Tag","Intent is empty");
        }
        else
        {
            widgetId = intentIn.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            allWidgetIds = intentIn.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            Log.d("Pref","id: " + widgetId + " ids: " + allWidgetIds.length);
        }

        final CheckBoxPreference checkboxPref1 = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref1");

//...

final Preference opt1 = (Preference) findPreference("settings1");
        opt1.setSummary("Widget's id: " + widgetId);

        opt1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
//...
}

@Override
    protected void onStop() {
        super.onStop();
        Intent infoIntent =  new Intent(DataAppWidgetProvider.FORCE_WIDGET_UPDATE);
        infoIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        this.sendBroadcast(infoIntent);
        finish();

    }

Do you know, what can be a problem? It looks like something is saved somewhere without my will and this is blocking everything.

4

1 回答 1

0

问题似乎在于您的小部件活动中使用了pendingintent。待处理的意图的特点是,如果您发送具有相同意图属性(如请求代码、意图等)的后续意图,那么即使您添加了新的附加内容,所有后续意图实际上都将返回相同的第一个意图。这就是为什么您必须看到仅第一次发送的数据就达到了您的偏好活动,并且在额外内容中具有不同值的后续意图没有任何影响。因此,如果您想使用相同的意图属性将更新的额外值发送到偏好活动,那么您必须使用一些标志,例如FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT指示有更改。请阅读有关待定意图的官方文档以了解这一点。

于 2013-07-09T13:06:10.337 回答