4

I developed an app that shows news feed on a home screen widget. Everything is OK on pre-Lollipop android devices as a result of the following scenario:

  • A user enters their launchers' widget screen to choose/add a specific widget
  • A user clicks on MyNewsWidget to add to their home screen
  • A configuration activity is called so user can choose from different news sources
  • A user clicks a "save" button inside the configuration activity, then the activity should finishes and MyNewsWidget should be added to their home screen.

I am testing my app on LG Nexus 5 device, but when I click to add my widget to the home screen after the configuration activity finishes it's work, the widget is not adding to home screen.

Here is my code for the Intent inside the Configuration activity:

int[] appWidgetIds = new int[]{mWidgetId};
Intent intent = new Intent(WidgetResourcesActivity.this, NewsWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
sendBroadcast(intent);
setResult(RESULT_OK, intent);
finish();

Also, this is my code inside my WidgetProvider class:

    @Override
    public void onReceive(Context context, Intent intent) {

        //I got this result on pre-Lollipop android devices "Action is: ACTION_APPWIDGET_UPDATE"
        if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
            int[] appWidgetIds = intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            for (int widgetId : appWidgetIds) {
                if (!isWidgetAdded(context, widgetId)) {
                    Intent widgetIntent = new Intent(Intent.ACTION_MAIN);
                    widgetIntent.setClass(context, WidgetResourcesActivity.class);
                    widgetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    widgetIntent.putExtra(WidgetResourcesActivity.EXTRA_WIDGET_ID, widgetId);
                    context.startActivity(widgetIntent);
                    PendingIntent pendingIntent = PendingIntent.getActivity(context, widgetId, widgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                } else {
                    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                    int[] widgetIds = new int[]{widgetId};
                    onUpdate(context, appWidgetManager, widgetIds);
                    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
                    updateButtonsStatus(remoteViews, 0, mFeedsCount);
                    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetIds, remoteViews);
                }
            }
            return;
        }

        //Here is my trouble . . .
        //Unfortuantely, I got this result on Lollipop android devices "Action is: ACTION_APPWIDGET_DELETED" !! Why? 
        if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_DELETED)) {
            if (BuildConfig.DEBUG) Log.e(TAG, "action : " + intent.getAction());
            removeWidgetId(context, intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
            return;
        }

        super.onReceive(context, intent);

        if (intent.getExtras() == null)
            return;

        int widgetId = intent.getExtras().getInt(EXTRA_WIDGET_ID);
        if (BuildConfig.DEBUG)
            Log.e(TAG, "action : " + intent.getAction() + " id : " + intent.getExtras().getInt(EXTRA_BUTTON_ID));

        if (intent.getExtras().getInt(EXTRA_BUTTON_ID) == R.id.widget_arrow_back) {
            mIndex = intent.getExtras().getInt(EXTRA_FEED_INDEX);
            if (mIndex > 0) {
                mIndex--;
                if (BuildConfig.DEBUG) Log.e(TAG, "currentIndex : " + mIndex);
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
                updateButtonsStatus(remoteViews, mIndex, mFeedsCount);
                updateWidget(context, remoteViews, mIndex, widgetId);
                handleWidgetBackButton(context, remoteViews, widgetId);
                handleWidgetNextButton(context, remoteViews, widgetId);
            }
        }

        if (intent.getExtras().getInt(EXTRA_BUTTON_ID) == R.id.widget_arrow_next) {
            mIndex = intent.getExtras().getInt(EXTRA_FEED_INDEX);
            if (mIndex < mFeedsCount - 1) {
                mIndex++;
                if (BuildConfig.DEBUG) Log.e(TAG, "currentIndex : " + mIndex);
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
                updateButtonsStatus(remoteViews, mIndex, mFeedsCount);
                updateWidget(context, remoteViews, mIndex, widgetId);
                handleWidgetNextButton(context, remoteViews, widgetId);
                handleWidgetBackButton(context, remoteViews, widgetId);
            }
        }
    }

Sadly, previous scenario is not working on android Lollipop (5.0 and later). Any good help would be much appreciated.

  • 1
    I also had a problem wherein an app widget for the Homescreen I created pre-lollipop didn't appear in widgets list. After reboot of device my app widget appeared in the widgets list. I'm not sure if it'll work for you. Someone in a community I'm in said that it's a bug in 5.0. – gilsaints88 Mar 30 '15 at 08:39
  • I had the same trouble, when I added new instance of appwidget I got "phantom" one but not real! It occured on Samsung Galaxy Note 3 with Android 5.0. But on emulators with Lollipop everything was Ok. Have you any idea how to fix it? – Alex Zezekalo Oct 06 '15 at 13:57

0 Answers0