John Mack
John Mack

Reputation: 103

RemoteViewsService onGetViewFactory not called

I'm sure I'm missing something simple here but onGetViewFactory is not calling while attempting to populate a StackWidget. I've gone over this multiple times and cannot find my error.

The RemoteViewsService is being attached in updateWidget:

public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId) {
    // Create the RemoteViews to represent the widget layout.
    Log.i(TAG, "updateWidget: Creating RemoteViews");
    RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.stack_widget_layout);

    // Create an intent that points to our widget service.
    Log.i(TAG, "updateWidget: Creating Intent");

    Intent remoteIntent = new Intent(context, StackWidgetService.class);

    // Attach that intent to the RemoteViews as our remote adapter.
    Log.i(TAG, "updateWidget: Setting Adapter");
    widgetViews.setRemoteAdapter(R.id.stack_view, remoteIntent);

    // Set which view should be used as the empty view.
    Log.i(TAG, "updateWidget: Setting empty view");
    widgetViews.setEmptyView(R.id.stack_view, R.id.text_empty);

    // Update the widget.
    Log.i(TAG, "updateWidget: Updating widget");
    appWidgetManager.updateAppWidget(widgetId, widgetViews);
}

The following should be called after calling appWidgetManager.updateAppWidget but is not:

 @Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    // NOT CALLED
    Log.i(TAG, "onGetViewFactory: Creating new view factory");
    return new StackWidgetViewFactory(getApplicationContext());
}

As far as I can tell, my manifest is correct:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".stackwidget.StackWidgetProvider"
        android:exported="true">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/stack_widget_info" />
    </receiver>

    <service android:name=".stackwidget.StackWidgetService"
        android:exported="true"
        android:permission="android.permission.BIND_REMOTEVIEWS"/>

</application>

And here is my stack_widget_info.xml, for safety:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp"
    android:minHeight="180dp"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/stack_widget_layout"
    android:autoAdvanceViewId="@layout/stack_item"
    android:widgetCategory="home_screen"
    android:resizeMode="horizontal |vertical">

</appwidget-provider>

Upvotes: 1

Views: 813

Answers (1)

John Mack
John Mack

Reputation: 103

The issue was actually in my widget layout. Constraint layout was causing issues. It runs correctly when changing over to Linear layout.

Upvotes: 1

Related Questions