android - Activity 暂停并重新创建后,如何停止 Activity 附加的 AlertDialog 继续出现在 Activity 上?

标签 android android-alertdialog android-dialogfragment

我正在做一个项目,它只是通过用户名和密码进行验证。

我在使用 DialogFragments 和 AlertDialog 方面取得了一些进展。在 mainactivity 上启动应用程序后会出现 AlertDialog,询问用户名和密码。 我必须设置 Alertdialog 的 setCanceledOnTouchOutside(false) 和 DialogFragment 的 setCancelable(false),因为我不希望用户通过按下 android 的后退按钮来关闭它。

问题是,在成功登录后以编程方式将其关闭后,如果该 Activity 再次变得不可见和可见,则会调用 Alertdialog 的 OnShowListener,再次显示此 AlertDialog。

我能否以某种方式从 Activity 中“分离”此 AlertDialog?解锁屏幕并返回 Activity 后也会出现此弹出窗口,这非常烦人...

这里是感兴趣的代码:

主要 Activity

public class MainActivity extends AppCompatActivity implements NoticeDialogFragment.NoticeDialogListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(GlobalInformations.getInstance().getUsername()==null){
        shownoticeDialog();
    }
}

public void shownoticeDialog(){
    DialogFragment dialogFragment = new NoticeDialogFragment();
    dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
}

@Override
public void onDismiss(DialogFragment dialog) {
   //set the username on a TextView instance, etc...
}

NoticeDialogFragment 扩展了 DialogFragment

public class NoticeDialogFragment extends DialogFragment {

public interface NoticeDialogListener{
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
    public void onDismiss(DialogFragment dialog);
}

NoticeDialogListener mListener;
static Activity activity = null;
//static String username;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        activity = (Activity) context;
        mListener = (NoticeDialogListener) activity;

    } catch (ClassCastException e){
        throw new ClassCastException(activity.toString() + "must implement NoticeDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog_signin, null);

    final AutoCompleteTextView actv_username = (AutoCompleteTextView) view.findViewById(R.id.username);
    final EditText password = (EditText) view.findViewById(R.id.password);

    getavailableusernames(actv_username);

    final AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(getContext(), R.style.AlertDialogCustom))
            .setView(view)
            .setTitle("Login")
            .setPositiveButton("OK", null)
            //.setNegativeButton("Cancel", null)
            .create();

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            final Button button =((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String passw = password.getText().toString();
                    String user = actv_username.getText().toString();
                    try{
                        if(user.length()<4 || passw.length()<4){
                            Toast.makeText(getContext(), "Username/password too short", Toast.LENGTH_SHORT).show();
                            dialog.show();
                        }
                        else {
                            //login to account, if success dismiss.
                          login(user, passw,dialog);
                        }


                    } catch(Exception e){

                    }
                    //  dialog.dismiss();
                }
            });
        }
    });
    dialog.setCanceledOnTouchOutside(false);

    // set the DialogFragment to make the dialog unable to dismiss with back button
    // (because not working if called on the dialog directly)
    this.setCancelable(false);

    return dialog;
}

 public void login(final String username, String password, final AlertDialog dialog){

    boolean login_success = false;
    //query the credentials
    login_success = dosomesqlquery(username, password);

    if(login_success){
         dialog.dismiss();
    }
 }

//passing the handling to activity...
@Override
public void onDismiss(DialogInterface dialog) {
    mListener.onDismiss(NoticeDialogFragment.this);
}

}

感谢您的帮助和耐心。

最佳答案

嗯,在这种情况下,我最终会一直坐在办公 table 前。

问题的根源是我调用了 dialog.dismiss() 来关闭对话框,但不是 dialogfragment 本身,所以永远不会关闭,即使对话框从屏幕上消失也是如此。将 this.dismiss() 放置在 NoticeDialogFragment 的 onDismiss 或登录成功后的任何其他地方,将使应用程序按其应有的方式运行。

@Override
public void onDismiss(DialogInterface dialog) {
    mListener.onDismiss(NoticeDialogFragment.this);
    this.dismiss(); //will dismiss the DialogFragment. Yeeey!
}

感谢您花时间和回答,因为它们帮助我指出了真正的问题。我会根据您的建议修改代码。

关于android - Activity 暂停并重新创建后,如何停止 Activity 附加的 AlertDialog 继续出现在 Activity 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747831/

相关文章:

android - Genymotion 模拟器增加堆大小

android - java.awt.AWTError : Toolkit not found: apple. awt.CToolkit Android appengineUpdate

java - WebView 滚动在 WindowManager 中不起作用

java - 软键盘不出现

android - 单击 DialogFragment 正按钮后如何执行操作

android - 检测 DialogFragment 中的用户不活动并在一段时间后注销

android - 如何在 API 22 中管理 Cookie

android - 在 Activity 之外的 Webview 中显示 AlertDialog

Android:AlertDialog 仅在第二次单击任何按钮后关闭

android - 在 DialogFragment 中单击 "Cancel"按钮后如何启动软键盘?