2011-09-20 74 views
2

我的應用程序的主要活動是首選項頁面。這是用戶單擊應用程序圖標時顯示的內容。我還有一項服務,可以發送用戶狀態欄通知,並且可以在屏幕上顯示半透明覆蓋圖。我遵循this發佈創建我的透明活動,所有這些工作。Android獨立透明活動

問題在於,無論何時,我都會顯示半透明活動,應用程序的主窗口(首選項頁面)在其後面可見。也就是說,半透明覆蓋圖顯示在當前正在運行的任何其他應用程序的頂部。

我該怎麼做才能使半透明活動出現時,我的應用程序中沒有其他活動可見?

這是我的主要活動在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> 

我也有半透明的疊加,它使用從Theme.Translucent派生的自定義主題:

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

這裏是佈局用於半透明覆蓋物:

<?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> 

而服務:

<service android:name="AppService"> 
    <intent-filter> 
    <action android:name="com.app.AppService" /> 
    </intent-filter> 
</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); 

感謝您的幫助

+2

這不就是透明的定義是什麼?你可以看到它背後的任何東西。 – nhaarman

+3

我同意尼克,你沒有提到你想要發生什麼。 – dmon

+0

你們都錯了,(我想)這個人有他的活動堆棧的問題,什麼擺脫所有的堆棧,並推出他的透明的東西;-) 查看答案。 –

回答

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> 
+1

,爲我工作,謝謝! – Shatazone

0

「問題是,曾經一次我顯示我的半透明的活動,應用程序的主窗口(首選項頁面)在它後面可見。「

你的問題是你有你的設置活動,並在它上面,TransparentACtivity。他們是在一個「堆棧」

當你打電話給你的通知,transparentActivity進來活動堆棧的前面(=設置)

如果你想看看會發生什麼「的背後」的transparentACtivity,你必須擺脫的籌碼是這樣的:

嘗試增加FLAG_ACTIVITY_CLEAR_TOP:

這次發射模式也可以用來結合良好的效果與 FLAG_ACTIVITY_NEW_TASK:如果用於啓動任務的根系活力, 它會將該任務的任何當前正在運行的實例帶到前臺,然後將其清除爲其根狀態。例如,在從通知 管理器啓動活動時,這尤其有用,例如: 。

所以你的代碼發動是:

Intent intent = new Intent(this, A.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 
+0

非常感謝您的回答。雖然它沒有像現在這樣工作,但我相信它讓我朝着正確的方向前進。 – oneself