AlertDialog не уволить на Android 6.0+ - PullRequest
0 голосов
/ 24 мая 2019

Android Studio 3.4

Мне нужно:

  1. Показать AlertDialog
  2. При нажатии на положительную кнопку скрыть диалоговое окно и показать индикатор выполнения
  3. Через 2 секунды скрыть индикатор выполнения

Вот фрагмент:

  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();
    }

И этот успех работает на Android 4.4 и Android 5.0

Но на Android 6.0+ при нажатии положительной кнопки AlertDialog не скрывается. В результате отобразится диалоговое окно с предупреждением и индикатор выполнения.

enter image description here

Через 2 секунды индикатор выполнения скрывается, и AlertDialog по-прежнему показывает:

enter image description here

Здесь макет для макета AlertDialog - 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>

Почему AlertDialog не закрывается при нажатии на положительную кнопку на Android 6.0+?

1 Ответ

1 голос
/ 24 мая 2019

Просто позвоните dismiss() в onClick

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