简体   繁体   中英

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. 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(); 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. 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. Now I use SkipStrategy to not pile up commands in the queue. Sorry for your time I spent :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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