【问题标题】:dismiss dialog when back to activity返回活动时关闭对话框
【发布时间】:2014-02-05 13:58:38
【问题描述】:

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

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

【问题讨论】:

  • 将标志添加到意图中。
  • 定义sometimes? (使用 startActivityForResult 检测您从浏览器返回的事实)
  • @Pankaj ,我添加了标志 Intent.FLAG_ACTIVITY_CLEAR_TOP,Intent.FLAG_ACTIVITY_NEW_TASK,它没有帮助。
  • @njzk2,从浏览器回来了

标签: android android-intent android-dialog


【解决方案1】:

这样试试:

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

另一种方法是采用静态布尔变量/首选项:

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

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

将此代码放入您的 onResume() 方法中:

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

【讨论】:

  • 如果调用完成()我的活动将被破坏。从浏览器返回时,我仍然需要显示我的活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多