I have a widget working with my app. When the app is first started I keep the context, and values so I can call my UpdateWidget() from the app when things change. But if the app doesnt know what the context and AppID is I dont know how to get that information. I think the way I tried to do this is wrong anyway, but not sure what else to try.
So basically the widget is already on the desktop. I start up the app and make some changes and I need to send an update to the app with the new info.
I would have thought the app would get this connection to the widget when it starts up, but not the case.
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
Ct = context;
ApMgr = appWidgetManager;
WidgetID = appWidgetIds;
WidgetOn = true;
UpdateWidget();
}
It works completely and without trouble.You can only use;
Unless that is the application context, you are leaking memory. Please do not leak memory.
Any place in your app where you could possibly need to update the widget, you already have a
Context
. For example:You absolutely know "what the context" is. It is your
Activity
. Or, if the code in question is in your customApplication
object, "the context" is theApplication
.With respect to the app widget ID, store it in a file. Or a database. Just be sure to clean it up when the app widget is uninstalled.
Or, if you are not supporting multiple app widgets, just skip the app widget ID altogether, and use the
updateAppWidget()
method that does not take an array of app widget IDs.I figured this out with the great help and clues from CommonsWare. What I was trying to do is make sure the app could send an update to the widget when something changes on the app database. there is no need to update the widget any other time. When the app gets disconnected from the widget from a reboot or new version the link was severed. this little function below gets the "link" back again. Thanks again to Stackoverflow and CommonsWare!! I hope I did this correctly, it does work the want I wanted it to.
I do this for my AppWidget without passing the AppWidget ID and it works fine.
or if you know the context you can do something like this -
Both works!!!