返回活动时关闭对话框

时间:2014-02-05 13:58:38

标签: android android-intent android-dialog

我按照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();再次获得相同的意图,然后再次显示对话框。

如何确保浏览器为对话框打开后,按下物理后退按钮时,对话框不再显示?

1 个答案:

答案 0 :(得分: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();
    }
相关问题