简体   繁体   English

AlertDialog再次打开

[英]AlertDialog is opened again

There is a fragment. 有一个片段。 When I press button on this fragment alert dialog is shown. 当我按此片段上的按钮时,将显示警告对话框。 This dialog is dissmissed after clicking on OK button. 单击“确定”按钮后,将关闭此对话框。 If I go to next fragment from current fragment and then come back - previous fragment is appeared with opened alert dialog. 如果我从当前片段转到下一个片段,然后再返回-上一个片段出现,并带有打开的警报对话框。 I use Cicerone for navigating. 我使用Cicerone进行导航。 Maybe somebody faced with this problem? 也许有人遇到这个问题?

// for navigating
router.navigateTo(screenKey);


// show dialog
AlertDialog alert = new AlertDialog.Builder(this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton(R.string.ok, (dialog, which) -> dialog.dismiss())
        .setCancelable(true)
        .create();
alert.show();


// in my second fragment
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    showBackButton();
}


// in my main activity
@Override
public void showBackButton() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(Utils.getDrawable(this, R.drawable.ic_arrow_back_white_24dp));
    toolbar.setNavigationOnClickListener(v -> {
        onBackPressed();
    });
}

@Override
public void onBackPressed() {
    hideKeyboard();
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        hideDrawerLayout();
    } else {
        super.onBackPressed();
    }
}
@Override
protected void onResume()
{
    super.onResume();
    dialog.close()
}

This will do the trick, in your parent activity where you are creating your dialog add this. 这将达到目的,在创建对话框的父级活动中添加此内容。 Try to comment and uncomment super.onResume(); 尝试注释和取消注释super.onResume(); while testing. 在测试时。

What i got that You are navigating in first line and in next step you are showing the Dialog. 我得到的是,您在第一行中进行导航,而在下一步中,您将显示对话框。 These will execute one after another. 这些将一个接一个地执行。 I Hope you got my point . 我希望你明白我的意思。 Do it as below : 如下进行:

 AlertDialog alert = new AlertDialog.Builder(this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton(R.string.ok, (dialog, which) ->
                dialog.dismiss()// Optinal
                router.navigateTo(screenKey);
        )
        .setCancelable(true)
        .create();
alert.show();

And no need to dismiss the dialog, cause its the default behavior of AlertDialog.Let me know its solved your problem. 无需关闭对话框,使其成为AlertDialog的默认行为。让我知道它解决了您的问题。 Thanks. 谢谢。

Well, I have find a solution. 好吧,我找到了解决方案。 Point is that Moxy framework is used in my project and I didn't set right state strategy type in my views. 关键是我的项目中使用了Moxy框架,并且我没有在视图中设置正确的状态策略类型。 Now I use SkipStrategy to not pile up commands in the queue. 现在,我使用SkipStrategy不在队列中堆积命令。 Sorry for your time I spent :) 抱歉,您花的时间:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM