Coder123
Coder123

Reputation: 854

widget only works after 2 installs or reboot

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!

Upvotes: 4

Views: 240

Answers (2)

Anis BEN NSIR
Anis BEN NSIR

Reputation: 2555

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);

Upvotes: 2

deadfish
deadfish

Reputation: 12304

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).

Upvotes: 2

Related Questions