Android 11 Updates: Conversations & Bubbles — part 2of 2
Part 2: Bubbles
This is the second of two posts about Android 11 Updates: Conversation Notifications and Bubbles. In the first post, we tried out new Conversation Notifications for my app, which is a chat between me and my virtual pet.
Now I’m going to look at how to quickly switch between different similar chat apps when you have multiple active conversations going on.
When you reach the end of this article, you will:
- Know how to build a chat app
- Have learned about Bubbles API
- Have learned about the settings that control Bubbles
The code is available in the Google repository, or, if you wish, you can go to Google Codelabs to play around with the new API.
Imagine having multiple conversations taking place in different chat messengers. It’s not convenient to chat via notifications or opening a chat screen via shortcut. Android 11 again brings us a solution called Bubbles API:
Image from the article.
Let’s start by answering what Bubble API is:
- The platform’s API that provides a way to publish content for apps
- A floating circle on top of everything
- Focused on one task
- An alternative to using SYSTEM_ALERT_WINDOW
The main thing to beware of when implementing this feature is not to bake the app’s whole functionality within the Bubble. For example, if it’s a chat messenger app, Bubble should only represent a chat screen with a particular person or group of people without the ability to navigate from it to another app’s screen.
OK, so now we know what Bubble is and what it looks like, so let’s add it to our app. First, we have to specify BubbleActivity in the AndroidManifest.xml that will represent chat with my virtual friend:
You may notice three new attributes in the activity tag. All of them are necessary to configure the Bubble:
- android:allowEmbedded=” true”, which means that the expanded Bubble will be embedded in the System UI.
- android:resizeableActivity=” true”, which means that the expanded Bubble will be resized by the System UI. By default, it is true from API 24.
- android:documentLaunchMode=”always” can have different values. I don’t want to focus on this too much because of the good documentation, but I’ll explain why I chose “always”. In my case, I want every Bubble to lead to its own chat screen so that every time I create a Bubble and tap it, a new BubbleActivity opens.
Let’s illustrate it with the useful Android tool dumpsys. Imagine that I created two Bubbles, two separate chats with my dog and cat, as in the picture:
So, if I run the command:
adb shell dumpsys activity package <package-id>
I’ll see the following result:
Activities:- ActivityRecord{df79488 com.example.android.people/.MainActivity }- ActivityRecord{2267175 com.example.android.people/.BubbleActivity }- ActivityRecord{8115093 com.example.android.people/.BubbleActivity }Recent Tasks:- Task{6b1e1c9 #379 visible=true type=standard mode=fullscreen translucent=false A=10183:com.example.android.people U=0 StackId=379 sz=1}- Task{66e250a #387 visible=true type=standard mode=multi-window translucent=false A=10183:com.example.android.people U=0 StackId=387 sz=1}- Task{a9e48d0 #388 visible=true type=standard mode=multi-window translucent=false A=10183:com.example.android.people U=0 StackId=388 sz=1}
That means that I have a separate chat activity for each Bubble. On a side note, you may notice that mode=multi-window is also a part of the configuration I did earlier (android:resizeableActivity=” true”)
OK, we defined the Activity in the manifest, but let’s take a look at the anatomy of the Bubble. For example, should we always use Activity for Bubbles?
The answer is ‘yes’. We have to start with the Activity, but underneath that, we can use whatever we want: fragment, view, or whatever else you prefer to use to build your screen. In my case, I used ChatFragment both for Bubble and when I navigate to ChatScreen in the app itself.
However, defining the activity is only half the work you have to do. Second, we have to link Bubble and Notification. Let’s get back to our notification builder and add information about Bubble:
- We create the Bubble intent to launch our activity:
- Then we can start combining all the details about Bubble with the Bubble Builder :) How lucky we are, builders everywhere:
- Now it’s time to link our Notification with Bubble metadata:
We can now launch the app to see if everything works as it should. Unfortunately, our app will crash with the following self-explaining exception:
Process: com.example.android.people, PID: 14483 java.lang.IllegalStateException: Created as a shortcut bubble, cannot set a PendingIntent. Consider using BubbleMetadata.Builder(PendingIntent,Icon) instead.at android.app.Notification$BubbleMetadata$Builder.setIntent(Notification.java:9190)at com.example.android.people.data.NotificationHelper.showNotification(NotificationHelper.kt:143)at com.example.android.people.data.DefaultChatRepository$sendMessage$2.run(ChatRepository.kt:119)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)at java.lang.Thread.run(Thread.java:923)
There are plenty of Builder constructors. I’m not sure why this one doesn’t work well. But let’s go back and build our Bubble according to the exception explanation:
val bubble = Notification.BubbleMetadata .Builder(intent, icon) .setDesiredHeightResId(R.dimen.bubble_height) .build()
The second try was a success! We managed to add a new icon in the notification that promotes this conversation to the Bubble:
In Android 11, Bubble Builder is API rich. I want to emphasize some of it:
- setAutoExpandBubble, which tells the system to expand the Bubble every time a new notification is received. Expanding means that, when you tap the Bubble, it’ll open BubbleActivity.
- setSuppressNotification, which sets whether the Bubble will be posted without the associated notification in the notification shade.
Remember, Bubbles were introduced in Android 10 but weren’t released. This feature has been hidden from normal users for almost a whole year. Why wasn’t it released?
Bubbles were discussed in an episode of the podcast Android Backstage (if you haven’t heard of this podcast before, I recommend that you give it a listen. It gets really technical). The team was asked the same question. The answer was more or less predictable: they wanted to know more about how to better integrate this feature into the system and fix some bugs in the meantime.
How could we try this feature on Android 10? Basically, if you run the code I showed you earlier on Android 10, you have to turn this feature on in developers’ options:
We’ve managed to display Bubble, but what is one of the main things Android developers should worry about when we talk about UI? The lifecycle.
When a Bubble is expanded, the content activity goes through the normal process lifecycle, resulting in the app becoming a foreground process (if it’s not already) (1).
You can remove the Bubble by tapping and holding it until you notice the X icon at the bottom of the screen. You can move your Bubble there, which removes the chat from the screen.
When the Bubble is collapsed or dismissed, the Activity will be deleted. This may lead to the process being cached and later killed if any foreground components of the app are running.
There are still some questions left before adopting Bubbles. What if we have multiple chats or multiple apps where we can chat with people? How many Bubbles can you have on the screen? Won’t it bombard the whole screen with Bubbles?
The answer is ‘no’. All Bubbles will be grouped in one point, as shown on the picture:
Another important question: How can users control Bubbles and notifications from a user perspective?
You can play around with a bunch of settings in your app’s notification settings menu:
For instance, in settings, you can mark a particular notification as not a Bubble, which will destroy that Bubble and won’t display it as a Bubble anymore until you manually turn it on again. I’ll let you play around with different options and move on to the summary.
I don’t want to make any conclusion about whether you need to implement it in your app, but I think that the key point is that Google is trying to unify APIs for apps that are used for chatting by introducing this new Bubbles API. For example, we have Facebook chat heads. Now they’re migrating it to be more Android-like. As far as I know, the System Alert Window permission, which is used quite a bit to build these communication channels, will soon be deprecated.
In addition, managing conversations will be much easier with Bubbles because you’ll spend less time switching between conversations from different apps. Everything will be in one place. And you have lots of control over when and how to display it.
***
At Flo, we work hard to bring real value to our users, just like Google does with Bubbles. So if you want to try out these new features in Android in our super app, please join our team.