简体   繁体   中英

AlertDialog not dismiss on Android 6.0+

Android Studio 3.4

I need:

  1. Show AlertDialog
  2. When click on positive button hide dialog and show progress bar
  3. After 2 seconds hide progress bar

Here snippet:

  private void showConfirmDialogSendPostalOffice() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.accept_terms_title);
        View customView = AndroidUtil.getLinearLayout(getActivity(), R.layout.terms_dialog);
        builder.setView(customView);
        builder.setPositiveButton(R.string.accept_terms, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dialog.cancel();
                ProgressService.getInstance(getActivity()).showProgress(getActivity(), "",
                        getString(R.string.processing) + ", " + getString(R.string.please_wait) + "...");

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ProgressService.getInstance(getActivity()).closeProgress();
                        Toast.makeText(getActivity(),
                                getActivity().getResources().getString(R.string.your_request_successfully_accepted), Toast.LENGTH_LONG).show();
                    }
                }, 2000);
            }
        });
        builder.setNegativeButton(R.string.cancel, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dialog.cancel();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

And this success work on Android 4.4 and Android 5.0

But on Android 6.0+ when I click positive button the AlertDialog not hide. As result show alert dialog and progress bar.

在此处输入图片说明

After 2 seconds progress bar is hide and AlertDialog still show:

在此处输入图片说明

Here layout for AlertDialog's layout - terms_dialog.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:text="@string/accept_terms_body"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/viewTerms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:paddingBottom="8dp"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dp"
        android:text="@string/view_terms"
        android:textSize="16sp" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="8dp" />

</LinearLayout>

Why AlertDialog is not dismiss when click on positive button on Android 6.0+ ?

Just call dismiss() in onClick

@Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //dialog.cancel(); remove this line
            }

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