2

My application's main activity is the preferences page. This is what gets displayed when the user clicks the application icon. I also have a service which sends the user status bar notifications, and can display a translucent overlay on the screen. I followed this post to create my transparent activity, and all of this works.

The problem is that ever time I display my translucent activity, the application's main window (the preferences page) is visible behind it. Meaning, that the translucent overlay appears on top of whatever other application is currently running.

How can I make so that when the translucent activity appears, no other activity from my application is visible?

This is my main activity's definition in AndroidManifest.xml:

<activity android:name=".AppPreferences" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

I also have translucent overlay which uses a custom theme which derives from Theme.Translucent:

<activity android:name=".AppPopupActivity" android:theme="@style/Theme.SemiTransparent">
  <intent-filter>
        <action android:name="com.app.HIDE_POPUP"></action>
    </intent-filter>
</activity>

Here is the layout for the translucent overlay:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <RelativeLayout android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true" >

        <Button android:text="@string/button_done"
                android:id="@+id/doneButton"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
        </Button>
    </RelativeLayout>
</RelativeLayout>

And the service:

<service android:name="AppService">
  <intent-filter>
    <action android:name="com.app.AppService" />
  </intent-filter>
</service>

To display the transparent activity I run the following in the service:

Intent intent = new Intent(this, AppPopupActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

Thanks you for your help

Community
  • 1
  • 1
oneself
  • 38,641
  • 34
  • 96
  • 120
  • 2
    Isn't that the definition of transparent? You can see anything behind it. – nhaarman Sep 20 '11 at 21:40
  • 3
    I agree with Niek, you didn't mention what do you _wanted_ to happen. – dmon Sep 20 '11 at 21:48
  • You are both wrong, (I guess) this guy has just issues with his activity stack and what to get rid of all the stack and launch his transparent stuff ;-) See answer. – Waza_Be Sep 20 '11 at 22:16
  • I am not wrong. The conclusion you came up with is nowhere to be found in the question. – nhaarman Sep 20 '11 at 22:20
  • Yes, you are right, we have to find the non-existing question :-p This seems to be this part: "The problem is that ever time I display my translucent activity, the application's main window (the preferences page) is visible behind it." – Waza_Be Sep 20 '11 at 22:26
  • Sorry, I will clarify the question. – oneself Sep 23 '11 at 15:54

2 Answers2

5

Although the answer by Profete162 bellow did not work, it led me in the right direction. After more reading and experimentation, I believe the right answer is to change the main activity's launchMode to "singleInstance" as follows:

<activity android:name=".AppPreferences" android:label="@string/app_name"
          android:launchMode="singleInstance">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
oneself
  • 38,641
  • 34
  • 96
  • 120
0

"The problem is that ever time I display my translucent activity, the application's main window (the preferences page) is visible behind it."

Your issue is that you have your settings activity and on top of it, the TransparentACtivity. They are in a "stack"

When you call your notification, the transparentActivity comes in front of the activity stack (= settings)

If you want to see what happens "behind" the transparentACtivity, you have to get rid of your stack like this:

Try adding FLAG_ACTIVITY_CLEAR_TOP:

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

So your code to launch A would be:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • Thank you very much for your answer. Although, it didn't work it as is, I believe it led me in the right direction. – oneself Sep 23 '11 at 17:34