Search code examples
androidwidgetrestore

Android Widget. How to restore process after force stop?


The process of my android widget has stopping after push "Force stop" in applications settings system menu or after "Close all" in Xiaomi MIUI. And widget stopping update. And there is a only one way to run again my widget - uninstall it and then install.

How to implement stable work for android widget?


Solution

  • I solved my issue. It turned out that process of widget doesn't stop when user pushing "Force Stop" in system menu or pushing "Clear All" in Xiaomi MIUI. Main thread stay alive and continue to receive system messages. But static fields of AppWidgetProvider which references to AppWidgetManager, RemoteViews и ComponentName becames NULL. I just do recreate new objects.

        public class AppWidget extends AppWidgetProvider {
        public static RemoteViews remoteViews;
        public static AppWidgetManager appWidgetManager;
        public static ComponentName watchWidget;
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
    
            if(remoteViews == null)
                remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
            if(appWidgetManager == null)
                appWidgetManager = AppWidgetManager.getInstance(context);
            if(watchWidget == null)
                watchWidget = new ComponentName(context, AppWidget.class);
        }
    }