Search code examples
androidandroid-asynctaskandroid-widgetscreen-orientation

Android AppWidget on orientation changed


I understand my question sounds old and I did search a lot on SO but could not figure out how to solve it.

I created an App Widget with a Refresh button which allow user to load information from Internet. Whenever user click Refresh, the widget will execute an Asynctask to download information.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for (int appWidgetId: appWidgetIds) {
           //update app widget
    }
}

@Override
public void onReceive(Context context, Intent intent) {

    if (REFRESH_LIST.equals(intent.getAction())) {
          //Execute AsyncTask to download information from Internet and update widget listview
    }
}

The widget is working perfectly except only when user click Refresh and rotate the screen while the AsyncTask is still running on background, then the whole widget settings and data of that widget instance are lost.

So anyone ever experienced same problem please cross me a finger. Thank you!!


Solution

  • After playing with this widget a while, I found that as long as the RemoteViews are not involved in the AsyncTask (I wanted to display a ProgressBar while downloading information) then the widget and AsyncTask should be running fine on orientation changed. Hope this could help anyone encountering same issues.