Tuesday, November 8, 2011

Two activities on the screen at the same time

Source code

Русский перевод.

Perhaps you've seen that some applications such as winamp, gimp and so on have several separated windows. It was interesting if it's possible to implement such a functionality on Android. Sure it could be done just with layouts, but we are not looking for easy ways.

First, let's define styles:

<style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>

    <style name="Theme.Transparent.Floating">
        <item name="android:windowIsFloating">true</item>

The "Theme.Transparent" style is like one we used for the splash screen but lacks "windowIsFloating" item. Due to this the first activity will fill the screen. And for the second activity there is "Theme.Transparent.Floating" style. So this activity won't fill the screen and our touches will be available for the first onу. No. By default activities are modal. And touches won't be available. We must do such workaround:


Ok, now we see the screen of the device, two activities and first activity is available behind the scene.
But there is one thing to do: communication between these activities. In normal workflow we use
startActivityForResult. But it's not applicable in this case. The simplest way to make them know one of another - to broadcast custom action:

mReceiver = new BroadcastReceiver() {

   public void onReceive(Context context, Intent intent) {
         // The first activity wants to close this one
         String operation = intent.getStringExtra("operation");


And as a result:

Maybe it will be usefull for something.

No comments:

Post a Comment