2

I built an application with the possibility to add widgets to the home screen. The widget is working with my Nexus 6P and a Motorola Moto G3.

With Samsung Phones (Tested with S3 mini (4.1.2), S5, S6 (6.0.1)) the widget is not added at all or TouchWiz crashes.

With another launcher (Nova) the widget is also not created on the S3 mini.

In logcat I don't see any error message at all.

I tried to narrow the problem down as far as possible. The widget gets created if I remove the android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity" from the counter_widget_info.xml. If I want to use a configuration activity TouchWiz crashes on the Samsung S3 mini.

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget_counter"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"
    android:previewImage="@drawable/widget_preview">
</appwidget-provider>

In the AndroidManifest.xml I register the widget with the following lines:

<receiver
            android:name=".widgets.CounterWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/counter_widget_info" />
</receiver>

On the Java side, I've got the following in the WidgetConfigurationActivity:

public class WidgetConfigurationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WidgetConfigurationBinding binding = DataBindingUtil.setContentView(this, R.layout.widget_configuration);
        setResult(RESULT_OK);
    }
}

And this in the WidgetProvider class:

public class CounterWidgetProvider extends AppWidgetProvider {
@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Intent intent = new Intent(context, CounterWidgetProvider.class);
        context.startService(intent);
    }
}
Marcel Bochtler
  • 1,291
  • 1
  • 14
  • 23
  • Just a heads up: I noticed some odd behavior I believe is from the tests I ran that caused the TouchWiz crashes. After rebooting, it seems TouchWiz somehow held onto references for widgets that never actually made it to the home screen, and started broadcasting updates for them. I just happened to notice because I ran tests with a pre-existing widget project that updates itself rather frequently, and shows a `Toast` each time. You might want to do a complete uninstall on any device you tested on, to clear that out, before continuing testing. – Mike M. Nov 22 '16 at 00:52
  • Did you find the proper solution? Because I'm facing with the same issue with you and accepted answer didn't work for me. – AtaerCaner Sep 04 '18 at 12:54
  • @AtaerCaner The solution doesn't work for me also – Nikita Kulikov Oct 17 '22 at 10:38

1 Answers1

2

From testing on my own Samsung devices, it seems the problem is that TouchWiz chokes if the result Intent doesn't have the App Widget ID attached, even if it's RESULT_CANCELLED.

From the App Widget developer page:

  • The App Widget host calls the configuration Activity and the configuration Activity should always return a result. The result should include the App Widget ID passed by the Intent that launched the Activity (saved in the Intent extras as EXTRA_APPWIDGET_ID).

It's unclear how exactly you wish to ultimately handle the result, but it's definitely a good idea to go ahead and set a valid result in onCreate(), in case the user backs out of the configuration Activity. For example:

final int id = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
final Intent result = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
setResult(RESULT_OK, result);
Mike M.
  • 38,532
  • 8
  • 99
  • 95