android - Activity 暂停并重新创建后,如何停止 Activity 附加的 AlertDialog 继续重新出现在 Activity 上?
问题描述
我正在做一个项目,它只是通过用户名和密码进行验证。
我在使用 DialogFragments 和 AlertDialog 方面取得了一些进展。AlertDialog 在通过 mainactivity 启动应用程序后出现,要求输入用户名和密码。我必须设置 Alertdialog 的 setCanceledOnTouchOutside(false) 和 DialogFragment 的 setCancelable(false) 因为我不希望用户通过按下 android 的后退按钮来关闭它。
问题是,在成功登录后以编程方式将其关闭后,如果活动再次变得不可见和可见,则调用 Alertdialog 的 OnShowListener,再次显示此 AlertDialog。
我可以以某种方式将这个 AlertDialog 从 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);
}
}
感谢您的帮助和耐心。
解决方案
好吧,这就是我最终不断地走向办公桌的那种情况。
问题的根源是我调用了 dialog.dismiss() 来关闭对话框,但不是对话框片段本身,因此即使对话框从屏幕上消失,也永远不会被关闭。登录成功后,将 this.dismiss() 放置在 NoticeDialogFragment 的 onDismiss 或其他任何地方将使应用程序按其应有的方式运行。
@Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
this.dismiss(); //will dismiss the DialogFragment. Yeeey!
}
感谢您的时间和回答,因为他们帮助我指出了真正的问题。我将根据您的建议修改代码。
推荐阅读
- android - Android 易受 JavaScript 接口注入问题的影响
- javascript - 使用javascript / typescript制作一个对象数组以具有3个数组的所有可能性组合
- ios - Swift:使用参数作为同一函数中不同参数的默认值
- node.js - Mongoose 保存多个子文档
- python - 如何从 datetime 模块中提取时间并增加它
- ansible - 使用 loop_var Ansible 循环多个变量
- bash - 如何调整 bash 文件以在单个节点上执行
- jenkins - 詹金斯档案 | 多环境的声明阶段
- c++ - 用于检查输入是否为闰年的 C++ 程序
- angular - 显示数组[]类型接口的值