4

My widget application only works if I install the widget add it to the screen and then install it again, if i add another widget i have to install again in order for the second one to start working (rebooting the device also helps, after the reboot all the widgets on the screen work, I have config file, and it does not reach my appWidgetProvider (the action is set on the onUpdate method), how can i force my APP to update the widget from the configuration file?

my entire project: https://github.com/vlad1001/Widget

Thanks!

Coder123
  • 784
  • 2
  • 8
  • 29
  • 2
    actually, where is nothing in your code that can make this behavior as you describe, it will be easier to help you, if you post that project on github. – HeyAlex Sep 21 '18 at 08:18
  • I uploaded the project to github, link above ^^ – Coder123 Sep 24 '18 at 16:12

2 Answers2

2

The only difference is see on your code is that your are finishing the activity before updating the widget. From documentation, the onUpdate method will not be called the first time. I think that your have to add the following:

super.onCreate(icicle);
setResult(RESULT_CANCELED);

Remove this line:

setResult(RESULT_CANCELED, resultValue);

After that, change the call to update before setResult and finish():

        //make the update before finish()
        appWidgetManager.updateAppWidget(appWidgetId, views);

        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();

I have not reproduce your issue, please let me know if this work for you.

After you share the source code, the base issue, is that on the first creation you are adding click intent to Text and on the update you add the pending intent to your imageView... changing this line resolve your issue. Test on the PR...

views.setOnClickPendingIntent(R.id.example_widget_imageview, clickPendingIntent);
Anis BEN NSIR
  • 2,555
  • 20
  • 29
  • 1
    could you please share your manifest, and full configuration activity code? Your code is similar to android sample, Could you please test if you can reproduce the issue on Sample application, in this case it may be a device issue... https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/appwidget – Anis BEN NSIR Sep 24 '18 at 09:09
  • hi, I uploaded my project to github: https://github.com/vlad1001/Widget – Coder123 Sep 24 '18 at 16:10
  • hello, i have checkout your project, and it seem to be working as expected. i install the widget, choose the time... it work well for multiple install, could you please explain more your issue? – Anis BEN NSIR Sep 25 '18 at 10:28
  • when you add the widget and choose the time, if you press on the widget it does not respond (only after a restart), does this work for you? – Coder123 Sep 25 '18 at 13:39
  • i tested on my phone and its working... Note: do not use setPendingIntentTemplate its used only when showing ListView on widget... – Anis BEN NSIR Sep 25 '18 at 15:33
2

My first shot. Take a look at file AndroidManifest.xml in Your project.

There is a line which might cause problem You have described.

android:allowBackup="true"

Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.

In short: uninstalling app doesn't mean You have uninstalled app's content and settings.

Try set it to false.

Related problem: https://stackoverflow.com/a/35296675/619673


Alternative: after first installation, clear cache of app, then run (or call in terminal adb shell pm clear <your.package.name).

deadfish
  • 11,996
  • 12
  • 87
  • 136