Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widget fails to launch properly after app has been force-stopped

I have a widget which launches to a particular page based on the various widget buttons pressed. It works fine when I have the app in-memory (i.e. from deploy (run / debug), however when I force stop the app, the widget no longer functions properly.

When the app has been force stopped, the first click of the widget works properly, and the onCreate for the corresponding Activity is called. Then, when the home key is pressed, and a different widget button is pressed, the onRestart method of the previous widget Activity is called.

It is odd, b/c the log messages do not appear, and since the debugger cannot be connected, it's been rather difficult to test. Is there anything in particular that I'm doing wrong?

The flow is --> WidgetDispatchActivity (handle true widget destination) --> ViewActivity> --> further branch (DetailViewActivity in below example)

onUpdate for widget:

onUpdate(){
...


       Intent viewIntent = new Intent(context, WidgetDispatchActivity.class);
                    viewIntent.putExtra("launch", WidgetInfo.VIEW_ACTIVITY);
                    PendingIntent viewPendIntent = PendingIntent.getActivity(context, 2, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);

...
}

WidgetDispatchActivity's onCreate:

 Bundle b = getIntent().getExtras();
        if (b != null && b.isEmpty()){
            Log.i(LOG_TAG, "EMPTY BUNDLE ON WIDGET!");
        }else{
            Log.i(LOG_TAG, "WIDGET BUNDLE HAS ITEMS!");
        }

        String destination = null;
        if (b != null && b.containsKey("launch")){
            destination = b.getString("launch");
            Log.i(LOG_TAG, "WIDGET ITEM FROM BUNDLE WAS: " + destination);
        }
        else{
            Log.i(LOG_TAG, " \"launch\" item was not inside bundle!");
        }

        if(destination != null) {
            Log.i(LOG_TAG, "PendingIntent received from widget!");
            Intent goTo = new Intent(this, ViewActivity.class);
               ...
}
}

LogCat

10-10 20:38:00.935: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
10-10 20:38:00.956: INFO/DetailViewActivity(1154): ONPAUSE
10-10 20:38:00.975: WARN/InputManagerService(58): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450366d0
10-10 20:38:02.545: INFO/ActivityManager(58): Starting activity: Intent { flg=0x10000000 cmp=com.starbucks.mobilecard/com.xxx.xxx.widget.WidgetDispatchActivity bnds=[198,298][224,321] (has extras) }
10-10 20:38:02.585: INFO/ViewActivity(1154): ONRESTART
10-10 20:38:02.585: INFO/ViewActivity(1154): WIDGET LAUNCH:: == false

ViewActivity onRestart:

protected void onRestart(){
        super.onRestart();
        Log.i(LOG_TAG,"ONRESTART");

        Log.i(LOG_TAG,"WIDGET LAUNCH:: == " + WidgetInfo.getInstance().wasLaunchedFromWidget());
                 ...
}

AndroidManifest:

<activity android:name="com.xxx.xxx.ui.cards.ViewActivity" android:label="@string/_view_title" android:finishOnTaskLaunch="true" android:clearTaskOnLaunch="true" android:screenOrientation="portrait"/>

<activity android:name="com.xxx.xxx.widget.WidgetDispatchActivity" android:label="@string/widget_dispatch_title" android:finishOnTaskLaunch="true"/>
like image 906
William Melani Avatar asked Nov 14 '22 16:11

William Melani


1 Answers

Willmel,

The first step in stopping your widgets from working when you Force Close is implementing your onDestroy(). Here you will want to make sure you talk to your WidgetHost, the HostViews and the RemoteViews that make up your Widget communications.

This statement stops the WidgetHost from receiving any events from Android. myHost is the object, often an Activity that is holding the references to your Widget.

// Stop the Widgets from fidgeting
    myHost.stopListening();
    myHost = null;

From there, you'll want to remove any reference to any of the HostViews you might have a reference to. This is done by simply grabbing the reference and setting it to null. In my onDestroy() I use the following loop...

if (appWidgets != null) {
    final int count = appWidgets.size();
    for (int i = 0; i < count; i++) {
        final Widget launcherInfo = appWidgets.get(i);
        launcherInfo.hostView = null;
    }
}

In here, launcherInfo is my extended AppWidget object. hostView refers to my AppWidgetHostView (which is the view that represents my Widget). The appWidgets array is my list of Widgets that my launcher is tracking. Pretty simple over all.

Working with Widgets and WidgetHosts can be problematic, depending on your set up. The above statements in the right place really help to ease the suffering.

FuzzicalLogic

like image 186
Fuzzical Logic Avatar answered Mar 09 '23 00:03

Fuzzical Logic