Invalid element passed activity from widget

I have a widget that uses a collection. I need to pass id from widget to action in my application when one of the widget rows is selected ListView.

For some reason, when I debug activity, an invalid identifier is sent. I narrowed it to a position. If I pass the position of my activity during debugging, this is the wrong position. Maybe this is just a misunderstanding of how the widget works with the collection, as this is my first.

Widget service

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory  {

        private List<Integer> mIds = new ArrayList<Integer>();
        private Context mContext;
        private int mAppWidgetId;

        public StackRemoteViewsFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        public void onCreate() {
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return mIds.size();
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public RemoteViews getLoadingView() {
            // TODO Auto-generated method stub
            return null;
        }

        public RemoteViews getViewAt(int position) {
            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
            final Intent mi = new Intent(mContext, MyActivity.class);
            final Bundle bun = new Bundle();
            bun.putInt("id", mIds.get(position));
            mi.putExtras(bun);

            final PendingIntent piMain = PendingIntent.getActivity(mContext, mAppWidgetId, mi, PendingIntent.FLAG_UPDATE_CURRENT);

            rv.setOnClickPendingIntent(R.id.widget_text, piMain);
            rv.setTextViewText(R.id.widget_text, mIds.get(position));

            return rv;
        }

        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        public void onDataSetChanged() {
            mIds = Utilities.GetIds();
        }

        public void onDestroy() {
            // TODO Auto-generated method stub

        }
}

My activity

public class Activity extends SherlockFragmentActivity {

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

        setContentView(R.layout.browser_pager);

        if (getIntent().getExtras() != null)
        {
            final Bundle extras = getIntent().getExtras();

            int id = extras.getInt("id"); //this id is wrong
        }
    }
}
0
source share
1 answer
final PendingIntent piMain = PendingIntent.getActivity(mContext, mAppWidgetId, mi, PendingIntent.FLAG_UPDATE_CURRENT);

getActivity() mAppWidgetId : . Intent "" PendingIntent . docs:

... , PendingIntent. , , PendingIntent Intents, "" , , PendingIntent. . , , , Intent.filterEquals. Intent, Intent.filterEquals, PendingIntent .

.

PendingIntent (, , ), , -, , PendingIntents. Intent, Intent.filterEquals , getActivity (, int, Intent, int), getActivities (, int, Intent [], int), getBroadcast (Context, int, Intent, int) getService (, int, Intent, int).

, , : position .

+2

Source: https://habr.com/ru/post/1546989/


All Articles