Android Wear: Activity in DisplayIntent of Notification Page gets always recreated

784 Views Asked by At

I'm developing some kind of media controls for my android wear. The scenario is the following:

The WearableListenerService in the wear app is notified when the media data changes, i.e. artist/title. An ongoing notification is shown which has a full screen second page with some controls in it. Because I don't want it to show on my smartphone, the notifications are build in the wearable app.

The second page consists of an activity baked in a display intent:

Intent pageIntent = new Intent(this, ControlActivity.class);
    pageIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(
            getApplicationContext(),
            0,
            pageIntent,
            0);

    Notification controlPage = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .extend(
                    new Notification.WearableExtender()
                            .setCustomSizePreset(Notification.WearableExtender.SIZE_FULL_SCREEN)
                            .setDisplayIntent(pendingIntent))
            .build();

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle(currentArtist)
            .setContentText(currentTitle)
            .setOngoing(true)
            .extend(
                    new NotificationCompat.WearableExtender()
                            .addPage(controlPage)
                            .addAction(new NotificationCompat.Action(button, null, intent))
                            .setContentAction(0)
                            .setContentIcon(button)
                            .setBackground(BitmapFactory.decodeResource(getResources(), bg))
                            .setHintHideIcon(true)
            )
            .build();
    NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notification);

Now the notification is refreshed from time to time to display new data on the first notification page which forces the activity in the second page to be recreated every time the notification is updated. This results in some ugly behavior where the user can see that the activity inside the second page is recreated.

Is there any way to prevent the activity in the second page from being completely recreated (onDestroy, onCreate) and/or only update the data on the first page of the notification as it should be? Is it the PendingIntent creation that doesn't really work here?

The Manifest entry looks as the following:

    <activity
        android:name=".media.ControlActivity"
        android:allowEmbedded="true"
        android:exported="true"
        android:launchMode="singleTop"
        >

I've tried all combinations of flags as well as only updating the necessary parts but nothing changes.

Help is greatly appreciated. Let me know if you need further information.

Edit: The main problem seems to be that the embedded activity (for a custom layout) in the second page of the notification is always recreated as soon as I call notify. Reusing, only updating changed values on the first page or updating nothing has no effect.

2

There are 2 best solutions below

1
On

there is no method to prevent recreate the embedded activity in the notification, you can try this way in your sence.

use a action in second page, and start activity when click the action, when the notification is updated by time, the activity will not be recreated

1
On

Simply broadcast the new information to your second activity and update the fields when you receive the broadcast. And make it local so the broadcast concerns only your application.

In the second activity add the BroadcastReceiver as a member attribute :
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { /*do what you're supposed to do, update your data you get the information from the intent by calling intent.getXExtra() as usual*/ } };

And register it in your onCreate() method :
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("your-filter-name"));

In your notification activity, when you update the information, add this :
Intent intent = new Intent("your-filter-name"); intent.putExtra(...); LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

Your second activity won't be recreated each time and the modifications should go smoothly :)