我正在研究项目,该项目仅通过用户名和密码进行验证。
我在使用DialogFragments和AlertDialog方面取得了一些进展。在mainactivity上启动应用程序后,出现AlertDialog并询问用户名和密码。 我必须设置Alertdialog的setCanceledOnTouchOutside(false)和DialogFragment的setCancelable(false),因为我不希望用户通过按下android的后退按钮来关闭它。
问题是,在成功登录后以编程方式将其关闭后,如果活动变得不可见并且再次可见,则Alertdialog的OnShowListener会调用,再次显示此AlertDialog。
我可以通过某种方式从活动中“分离”此AlertDialog吗?解锁屏幕并恢复活动后,也会弹出此弹出窗口,这非常烦人...
这是您感兴趣的代码:
MainActivity
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);
}
}
感谢您的帮助和耐心。
答案 0 :(得分:1)
在这种情况下,我最终要连续向办公桌走去。
问题的根源是我叫dialog.dismiss(),它关闭了对话框,但不是对话框片段本身,所以即使对话框从屏幕上消失了也永远不会关闭。将this.dismiss()放置在NoticeDialogFragment的onDismiss或成功登录后的任何其他位置,将使应用程序按其应有的方式运行。
--xmlout
感谢您的时间和回答,因为它们帮助我指出了真正的问题。我将根据您的建议修改代码。
答案 1 :(得分:0)
设置活动进入后台时的值
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("authUser", GlobalInformations.getInstance().getUsername()==null)
}
并在回来时阅读
@Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null && savedInstanceState.containsKey("authUser")) {
boolean authUser = savedInstanceState.getBoolean("authUser", false);
if(authUser) {
//show or don't show dialog
}
}
}
答案 2 :(得分:0)
一种更简单的方法是通过两个步骤在您的活动中使用静态变量。
声明全局静态布尔
private static boolean session = false;
检查布尔值是否已更改,如果没有更改,则在显示对话框时将布尔值设置为true
public void shownoticeDialog(){ 如果(会话)返回; DialogFragment dialogFragment =新的NoticeDialogFragment(); dialogFragment.show(getFragmentManager(),“ NoticeDialogFragment”); session = true; }