Android 返回“活动”时关闭对话框

Android 返回“活动”时关闭对话框,android,android-intent,android-dialog,Android,Android Intent,Android Dialog,我按照命令创建了一个通知 当顶部栏上显示通知时,我单击该通知并打开我的活动。在MyActivity的onCreate()中,我通过通知获取设置的意图,并显示一个对话框 public class MyActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //get

我按照命令创建了一个通知

当顶部栏上显示通知时,我单击该通知并打开我的活动。在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);

       }
    });
   }

}
显示的对话框上有一个“打开”按钮,当单击该按钮时,浏览器打开。这里一切都很好

如果现在按下physical back按钮,浏览器将消失,MyActivity将再次显示然而,,再次调用
onCreate()
(有时),其中
Bundle extras=getIntent().getExtras()再次获得相同的意图,对话框再次显示

如何确保在为对话框打开浏览器后,当按“物理后退”按钮时,对话框不会再次显示

这样试试:

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()方法中:


将标志添加到intent.define
有时
?(使用startActivityForResult检测您从浏览器返回的事实)@Pankaj,我添加了flag Intent.flag\u ACTIVITY\u CLEAR\u TOP,Intent.flag\u ACTIVITY\u NEW\u TASK,它没有帮助。@njzk2,它从浏览器返回。如果调用finish()我的活动将被销毁。从浏览器返回时,我仍然需要显示我的活动。
    if(isNotify)
    {
         isNotify=false;
         showDialog();
    }