2014-02-05 23 views
0

我跟着Android Developer document創建通知解僱對話時回到活動

當通知被頂欄上顯示,我點擊該通知,我的活動被打開。在MyActivity的onCreate()中,我通過通知獲取意圖集,並顯示一個對話框。

public class MyActivity extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     //get extras from intent from notification component 
     Bundle extras = getIntent().getExtras(); 
     if(extras != null){ 
      showDialog(); 
     } 
    } 

    private void showDialog(){ 
     ... 
     //There is a "open" button on the dialog, which opens browswer 
     openButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //I dismiss the dialog 
      dismissDialog(); 

      //Then I open the browser 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl)); 
      startActivity(browserIntent); 

     } 
    }); 
    } 

} 

在顯示的對話框中有一個「打開」按鈕,當點擊該按鈕時,瀏覽器打開。一切工作正常。

如果現在按下物理後退按鈕,瀏覽器消失,MyActivity會再次顯示。 然而onCreate()被再次調用(有時),其中Bundle extras = getIntent().getExtras();再次獲得相同的意圖,並再次顯示對話框。

如何確保瀏覽器打開對話框後,按下物理後退按鈕時,對話框不再顯示?

+0

將標誌添加到意圖。 –

+0

定義'有時'? (使用startActivityForResult來檢測您從瀏覽器回來的事實) – njzk2

+0

@Pankaj,我添加了標誌Intent.FLAG_ACTIVITY_CLEAR_TOP,Intent.FLAG_ACTIVITY_NEW_TASK,它沒有幫助。 –

回答

0

嘗試像這樣:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl)); 
finish();   
startActivity(browserIntent); 

另一種方式是採取靜態布爾變量/ prefrence:

static boolean isNotify=false; 
Bundle extras = getIntent().getExtras(); 

     if(extras != null) 
     { 
      isNotify= true; 
     } 

將這個代碼在你的onResume()方法:

if(isNotify) 
    { 
     isNotify=false; 
     showDialog(); 
    } 
+0

回來,如果調用finish()我的活動會被破壞。從瀏覽器返回時,我仍然需要顯示我的活動。 –

+0

看到馬更新的答案 –

相關問題