Leverage Multi-Window and Activity Embedding

[ad_1]

With Android 12L, we put an effort into making apps better on large screens, and with Android 13 we introduced more new opportunities to enhance the experience you can provide to your users. In a world with increased screen real estate, multitasking is important, and here are two ways you can leverage it:

You probably already know that your users can get easily into multi-window mode with a simple gesture, but you can start multi-window mode yourself by using the Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT flag in the intent used to launch the target app:

This function will launch OtherActivity adjacent to yours in multi-window, so that it can leverage multitasking. One piece of advice: this will work on every device running Android 7 and newer, and it will split the screen into two halves, either vertically or horizontally, based on the device. Additionally, starting from Android 12L, using the resizableActivity="true"flag will not opt out from multi-window.

How multi-window appears on phones and tablets

Many apps means many architectures, but there might be cases where you need to launch another app from yours for different and valuable reasons. For instance, your business might be focused on different services that can work together, like a file manager and a spreadsheet app, a news reading and a sports news app, streaming media and a chat app, and so on.

You are probably already familiar with activity embedding (if not, make sure to check out this talk), and in Android 13, we introduced the possibility of launching activities from other apps as if they originated from your own app.

Since embedding other apps’ activities make them appear as if they are a part of your app, there are additional security requirements to use this form of embedding. First and foremost, enabling activity embedding across apps is an opt-in feature, meaning that the embedded app needs to take action to be embedded.

The embedded app can opt-in by:

  • allowing any and every app to embed the activity, and performing any trust verification at runtime
  • embedding a list of certificates in the XML resources and Android will only allow apps signed with one of those certificates to embed it

In the sample on GitHub, you will see how to let any app embed yours and how to embed an external app in yours. But in this example, no safety check will be performed, since the goal of this post is to explain how the API works and how you can troubleshoot issues that might arise.

Let’s start from the app you want to embed. To allow everyone to embed an activity, you need to make the activity visible from outside the APK. Thus, you need to set the exported flag to true, and you need to add the following line to the manifest, so that it looks like this:

The last line is the one that tells Android that your activity can be embedded.

Note: Remember, for the purpose of this blog post, you are not setting up any security measures.

Now that you have your app ready to be embedded, it’s time to embed it in another app of yours!

For the sake of clarity, you can think of the embedded app as the guest and the embedding app as the host.

First thing you need to do is add the following dependencies to the host app’s build.gradlefile:

Next, you need to create a split configuration that will allow you to define which Activity you want to embed using its qualified name:

The SplitPairRule defines the behavior of the two activities when one finishes, ensuring the navigation you intend. In this case, if the host activity finishes, the guest will finish as well, but it won’t be the same in case the guest finishes first. Next, the rule defines the breakpoint for showing the activities side by side, which is 600dp in this case. Lastly, you define the split ratio from 0 to 1; 0.3 means that the host activity will take 30% of the available space on screen, and the guest activity will take up the rest of the available space.

The SplitPairFilter defines what activities are involved, using the simple name for the host, and the qualified name for the guest.

Additionally, you can declare a placeholder that will be presented before the guest activity is started or after it’s finished, by using the SplitPlaceholderRule:

At this point, you need to create the startup initializer that will load the split configuration:

Then, you need to add the initializer to the AndroidManifest file of the host app to make it discoverable:

The last thing you need to do is call the guest Activityfrom the host app:

If you want to know more about activity embedding, check out the sample on GitHub, the video from Android Dev Summit ’22, and the Activity embedding developer guide.

[ad_2]

Source link


Leave a Reply

Your email address will not be published. Required fields are marked *