在启动画面中检查条件,如果条件为假,将出现对话框。 在拨号日志框中,如果用户是接受条件共享首选项布尔值将为true如果用户单击取消,则SharedPref布尔值将为false。 用户接受条件后,功能打开应用程序中没有“需要显示”对话框。
对我来说打开应用程序对话框即将到来,因为总是“ False ”布尔值即将到来。 我无法理解我错误的地方, 请你帮助我好吗。
Boolean value is getting false every time.
public static final String Alert_Dialog = "AlertDialog";
static SharedPreferences settings;
public static SharedPreferences.Editor editor;
settings=getSharedPreferences(Alert_Dialog, 0);
editor = settings.edit();
if(settings.getBoolean("Alert_Dialog", false)==false){
Toast.makeText(SplashScreen.this,"False cond=="+settings.getBoolean("Alert_Dialog", false),Toast.LENGTH_LONG).show();
UserAgreement();
}else if(settings.getBoolean("Alert_Dialog", false)==true){
Toast.makeText(SplashScreen.this,"True cond=="+settings.getBoolean("Alert_Dialog", false),Toast.LENGTH_LONG).show();
Intent intentHActivity = new Intent(this, HActivity.class);
startActivity(intentHActivity);
}
@SuppressLint("StringFormatInvalid")
private void UserAgreement() {
final Dialog openDialog1 = new Dialog(context);
openDialog1.setContentView(R.layout.dialog_box_layout);
Button dialogAgreeButton = (Button)openDialog1.findViewById(R.id.dialog_agree_button);
TextView cancleTV1= (TextView)openDialog1.findViewById(R.id.dialog_cancle_tv);
dialogAgreeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putBoolean("Alert_Dialog", true); Toast.makeText(SplashScreen.this,"accept==="+settings.getBoolean("Alert_Dialog", false),Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, nextActivity.class);
startActivity(intent);
}
});
cancleTV1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putBoolean("Alert_Dialog", false);
Toast.makeText(SActivity.this,"cancle==="+settings.getBoolean("Alert_Dialog", false),Toast.LENGTH_LONG).show();
moveTaskToBack(true);
finish();
}
});
openDialog1.show();
openDialog1.setCanceledOnTouchOutside(false);
openDialog1.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
if(keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP)
{
moveTaskToBack(true);
finish();
}
return false;
}
});
editor.commit();
}
答案 0 :(得分:0)
检查您的dialogAgreeButton.setOnClickListener()
您忘了在SharedPreferences
editor.apply();
或editor.commit()
提交SharedPreferences
<强> FYI 强>
每当您更改
SharedPreferences
中的值时,使用commit()
或apply()
提交您的偏好设置,请从此编辑器更改为正在编辑的SharedPreferences
对象
像这样更改你的代码
dialogAgreeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putBoolean("Alert_Dialog", true);
editor.apply();
Toast.makeText(SplashScreen.this,"accept==="+settings.getBoolean("Alert_Dialog", false),Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, nextActivity.class);
startActivity(intent);
}
});
答案 1 :(得分:0)
你应该在像editor.apply()
public void onClick(View v) {
editor.putBoolean("Alert_Dialog", true);
editor.apply()
Intent intent = new Intent(this, nextActivity.class);
startActivity(intent);
}
注意:在edit.putBoolean(&#34; Alert_Dialog&#34;,true)之后立即 editor.apply()
答案 2 :(得分:0)
Editor.commit()调用在实际的putBoolean之前完成。
putBoolean仅被称为onClick,但是当对话框出现时,就已经执行了提交调用,这是在进行任何更改之前。
只需将其称为main queue
,就可以了。