Android独立透明活动

one*_*elf 2 android android-activity

我的应用程序的主要活动是首选项页面.这是用户单击应用程序图标时显示的内容.我还有一个服务,它发送用户状态栏通知,并可以在屏幕上显示半透明的覆盖.我按照这篇文章创建了我的透明活动,所有这些都有效.

问题是,无论何时我显示我的半透明活动,应用程序的主窗口(首选项页面)都可以在其后面看到.这意味着,半透明叠加层显示在当前正在运行的任何其他应用程序之上.

如何在半透明活动出现时,我的应用程序中没有其他活动可见?

这是我在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>
Run Code Online (Sandbox Code Playgroud)

我也有半透明叠加,它使用源自Theme.Translucent的自定义主题:

<activity android:name=".AppPopupActivity" android:theme="@style/Theme.SemiTransparent">
  <intent-filter>
        <action android:name="com.app.HIDE_POPUP"></action>
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

这是半透明叠加层的布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

和服务:

<service android:name="AppService">
  <intent-filter>
    <action android:name="com.app.AppService" />
  </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

要显示透明活动,请在服务中运行以下命令:

Intent intent = new Intent(this, AppPopupActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

one*_*elf 5

虽然Profete162的答案不起作用,但它使我朝着正确的方向发展.经过更多的阅读和实验,我认为正确的答案是将主要活动的launchMode更改为"singleInstance",如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)