3

(跳過簡要摘要)我在應用程序中的活動序列遇到問題。該應用程序有3個活動,從A流到B到C(或從C到B到A,如果連續按下)。在正常的操作中,應用程序工作正常,我遇到的問題是收到C2DM通知時。通知,PendingIntent和意圖標記問題

收到通知後,我希望將PendingIntent發送給當前活動的活動,或者在應用程序未運行的情況下啓動應用程序。

在通知接收者中,PendingIntent是用任何當前活動(在Application類中保存的)創建的,這也可以正常工作。

但是,當活動從創建PendingIntent的活動發生變化時出現問題。

即在Activity C中收到通知,並且PendingIntent是通過指向Activity C的Intent創建的。用戶返回到Activity A並單擊通知,觸發該intent並啓動Activity C,並且我希望它發送到A.

我想要的是將意圖發送到當前的任何活動。我已經嘗試創建一個Activity Pe作爲PendingIntent的目標,然後在創建Activity X時檢查存儲在Application類中的哪個Activity當前處於活動狀態。

但我在這裏也遇到了問題與意圖旗幟。我想使用SINGLE_TOP標誌的意圖,我將用來確保onNewIntent調用當前的活動,這樣我就不會有代碼在onCreate和onResume 。

我在這裏遇到的問題是Activity X現在處於活動堆棧的頂部,而單個top並不是因爲這一點。我已經嘗試使用NO_HISTORY和EXCLUDE_FROM_RECENTS標誌作爲掛起的意圖,以便Activity X不會添加到堆棧中,然後使用SINGLE_TOP來啓動活動A,B或C,但它創建它們的新實例而不是使用那個存在。

總結:

4 activities: A,B,C,X and a receiver for notifications. 

on receipt of a notification: 
    //not sure which is better to use or even if they're right for my purpose 
    start X with a PendingIntent with FLAG_ACTIVITY_NO_HISTORY or FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 

    Acivity X then gets the currently running activity from Application (A,B or C) 
     create an intent for the current activity (A,B or C) with FLAG_ACTIVITY_SINGLE_TOP 

But onNewIntent is never called a new instance of A,B or C is created. 

有沒有更合理的方法解決這個問題,是我錯了使用標誌?我是否理解了關於意圖工作方式的基本理念,對此我將不勝感激。對於長期的問題感到抱歉。此外,如果需要代碼,我可以明天發佈,因爲我離開了我的工作站。

回答

4

答案蘇希給了我一個嘗試設置清單中的值的想法。

所以在清單中的活動X

android:noHistory="true" 
android:launchMode="singleInstance" 

,並在清單的Actvities A,B和C

android:launchMode="singleTop" 

我還是用我的懸而未決的意圖,雖然

的NO_HISTORY標誌
startActivity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      startActivity, PendingIntent.FLAG_UPDATE_CURRENT); 

並且在活動X中然後將意圖發送給A,B或C

//onCreate 
    myApp = (MyApplication)getApplication(); 
    String s = myApp.getCurrentActivity(); 

    if(s.equals(myApp.ACTIVITY_A)){ 
     Intent intent = new Intent(this,ActivityA.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     startActivity(intent); 
    } 

    if(s.equals(myApp.ACTIVITY_B)){ 
     Intent intent = new Intent(this,ActivityB.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     startActivity(intent); 
    } 

    if(s.equals(myApp.ACTIVITY_C)){ 
     Intent intent = new Intent(this,ActivityC.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     startActivity(intent); 
    } 
    finish(); 

通知是經由廣播接收器接收的,待決意圖被髮送到基於當前活動的名稱(在應用保持)活性X發送的意圖的當前活動的活性和onNewIntent方法接收。

+0

太棒了。很高興它有幫助! – Suchi 2011-12-14 14:02:36

0

你試過android:noHistory

+0

剛剛嘗試過,但沒有奏效,但它確實讓我找到了正確答案。 – triggs 2011-12-14 10:28:25