4

I found recently that after I renamed my class that extends AppWidgetProvider (as part of a code tidy-up), and installed the modified app, existing widgets would break, displaying just "Problem loading widget".

Adding a fresh widget to the home screen works fine, it's just existing widgets that break. Whilst for me as developer I don't mind deleting existing widget and adding a new one, the users of my app would not take kindly to having to do this because each widget takes time to reconfigure.

So, is there a way of safely renaming an AppWidgetProvider class without breaking existing widgets? I could of course just keep the name as it is, but it gives me an uneasy feeling that I'm stuck with the (in hindsight slightly confusing) name forever.

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

1 Answers1

2

Unfortunately, there is no way to fix it. The problem is in changed ComponentName. AppWidgetHost just trying to get your widgets from AppWidgetManager, and AppWidgetManager trying to get previous widgets by ComponentName, so after changing package name of yours AppWidgetProvider, AppWidgetHost can't restore your previous widgets, because he got saved old ComponentName. So as you can see, there is no way to change a package name of AppWidgetProvider.

HeyAlex
  • 1,666
  • 1
  • 13
  • 31
  • :-0 just wish I'd given it a better name to start with! no big deal, really, just a little bit frustrating to be stuck with a class name forever (or face the wrath of app users). – drmrbrewer Dec 26 '18 at 12:27
  • ye, widgets so unpopular, so they still have really bad api. – HeyAlex Dec 28 '18 at 12:23
  • 1
    This should be marked as a correct answer. I exactly had the same issue and now I figure out why. – Doctiger Aug 05 '19 at 10:46
  • Although you are right that there is no way to fix it, the problem is within the *AppWidgetService* and not the *AppWidgetHost*. The former listens to broadcasts related to application packages (like adding, removing, updating). When you update your app with a new provider under a new *ComponentName*, it removes the previous one because it is no longer referenced anywhere. I wrote a [blog post](https://arkadiuszchmura.com/posts/do-not-change-the-package-name-or-class-name-of-your-app-widget-provider/) about this problem. – CloudJR Feb 22 '22 at 06:56