16

I'm having the hardest time figuring out how to remove home screen AppWidget's programmatically (i.e. without the user actually dragging one into the trash). As an example, consider an app that can have multiple accounts, with any number of widgets for each account - once an account is removed, widget should be deleted as well.

I've tried following an obscure example from http://www.netmite.com/android/mydroid/cupcake/frameworks/base/services/java/com/android/server/AppWidgetService.java, but that doesn't seem to even trigger OnDeleted, much less remove the AppWidget from the home screen.

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED);
intent.setComponent(info.componentName); // references AppWidgetProvider's class
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
sendBroadcast(intent);

Does anyone have any advice on how this can be accomplished? An example would be the bee's knees. Thanks.

Melllvar
  • 2,056
  • 4
  • 24
  • 47

2 Answers2

28

You cannot add or remove app widgets from the home screen. Only the user can do that.

Any app widgets tied to a deleted account could show a different account, or adopt some "(account deleted)" look that would trigger the user to get rid of the app widget or reconfigure it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks, Mark. Great book, by the way. – Melllvar Dec 26 '10 at 07:15
  • @CommonsWare sir how to know user remove widget so background process stop? – Harshid Mar 06 '13 at 07:56
  • 1
    @Harshid: Set up your `AppWidgetProvider` to respond to `onDeleted()` and/or `onDisabled()`. You will need the corresponding actions in your `` for the `` element corresponding with the `AppWidgetProvider`. – CommonsWare Mar 06 '13 at 12:15
  • @CommonsWare Sir, If I want to delete widget (which is created by me) on widget's click , than it's possible or not ? I am trying to delete my created widget on widget's click. – Harsh Trivedi Oct 08 '13 at 07:24
  • @HarshTrivedi: Only the home screen can add and remove app widgets. – CommonsWare Oct 08 '13 at 10:56
  • @CommonsWare Thanks for reply. So by code (click event) i can not remove any widgets from home screen right ? – Harsh Trivedi Oct 08 '13 at 12:31
  • @HarshTrivedi: Correct, unless that code is the home screen itself. – CommonsWare Oct 08 '13 at 12:31
  • @CommonsWare Ok, I am working to make Launcher and I am trying to delete widget by doing this code " appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);" but i cant able to actually delete – Harsh Trivedi Oct 08 '13 at 12:38
  • 1
    @HarshTrivedi: I would suggest opening up a separate question for that. Home screens clearly can get rid of app widgets, but I personally do not know the mechanism by which they do that. – CommonsWare Oct 08 '13 at 12:41
  • 1
    @CommonsWare I have checked in the samsung device, When I drag and drop widget of "Contact", An activity popped out, when we select any contact, then the widget will setup, but if we exit from the activity withot selecting any contact, then the widget will automatically remove. So can you please tell, how they have manage it, If we can't remove widget from the home screen. Thanks – Kimmi Dhingra Apr 21 '16 at 10:38
  • 2
    @ErKimmiDhingra: You are referring to the [app widget configuration activity](http://developer.android.com/guide/topics/appwidgets/index.html#Configuring). If the user BACKs out of that activity, the system does not add the app widget in the first place. The developer of the app widget is not removing anything. – CommonsWare Apr 21 '16 at 11:06
  • @CommonsWare Thanks a lot. You really save my day – Kimmi Dhingra Apr 21 '16 at 12:31
  • @CommonsWare can you please let me know, how I can manage multi instance of widgets. I have to make a widget same like "Contact" widget – Kimmi Dhingra Apr 21 '16 at 12:38
-1

I'm pretty sure this should work:

int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName("com.example",
                        "com.example.Widget"));
AppWidgetHost host = new AppWidgetHost(ctx, 0);
host.deleteAppWidgetId(appWidgetIds[0]);
christoff
  • 587
  • 1
  • 6
  • 17
  • 12
    Unfortunately, that does not work because deleteAppWidgetId() does not remove the widget from the home screen but just [stops listening to changes for this AppWidget](http://developer.android.com/reference/android/appwidget/AppWidgetHost.html#deleteAppWidgetId%28int%29). – sschuberth Jun 28 '12 at 12:53