Skip to content Skip to sidebar Skip to footer

Start Mainactivity From Appwidget Doesn't Work

I'm trying to launch my MainActivity from my AppWidget with pending intent but I can't figure out why it doesn't fire the intent. The widget has a ListView with up to 3 items, whic

Solution 1:

I think I found the answer, or at least a workable solution in a related post:

AdapterViewFlipper in app widget: setPendingIntentTemplate() and setOnClickFillInIntent() not working

It turns out I wasn't setting the FillInIntent properly. I should call setOnClickFillInIntent on the top level view of the list item instead of the list itself. Here's the code change that made it work in RemoteViewsFactory:

public RemoteViews getViewAt(int position) {
        if (DEBUG) Log.d(LOGTAG, "StockRemoteViewsFactory getViewAt position="+position);

        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.text_symbol, mStockItems.get(position).getSymbol());
        rv.setTextViewText(R.id.text_price, String.format("$%.2f", mStockItems.get(position).getPrice()));

        Intent fillInIntent = new Intent(Intent.ACTION_MAIN);
        fillInIntent.putExtra(StockAppWidgetProvider.ITEM_NUM, position);

        // set on the top level view of the list item, instead of the list view itself//rv.setOnClickFillInIntent(R.id.stocks, fillInIntent);
        rv.setOnClickFillInIntent(R.id.general_layout, fillInIntent);    

        return rv;
    }

Post a Comment for "Start Mainactivity From Appwidget Doesn't Work"