alertDialog disclaimer shows on every run
I want to add an disclaimer to my app which pops up at the first start of
the app. If the User declines, the app closes. If the user opens the app
again the disclaimer should pop up again until the user accepts it. Once
accepted it should hide and don't pop up anymore.
My problem: If I accepted the disclaimer it closes and everything is fine.
But when I launch the app it pops up again.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences pref = getSharedPreferences("Preferences",
MODE_PRIVATE);
String lver = pref.getString("Version", "");
String ver = this.getString(R.string.version);
if(ver != lver)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Disclaimer")
.setMessage(this.getString(R.string.disclaimer))
.setCancelable(false)
.setIcon(R.drawable.caution)
.setPositiveButton("Accept", new
DialogInterface.OnClickListener() {
SharedPreferences.Editor edit = pref.edit();
public void onClick (DialogInterface dialog, int id) {
boolean accepted = true;
dialog.cancel();
if(accepted == true)
{
edit.putString("Version",
this.getString(R.string.version));
edit.commit();}
}
private String getString(int version) {
// I had to create this method, cause i got an error
the line above this.getstring(R.string.version)
return null;
}
})
.setNegativeButton("Decline", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
});
AlertDialog disc = builder.create();
disc.show();
} }
I found a quite similar question One-time AlertDialog showing on every run
But I couldnt solve my problem with it.
I would be really happy about an answer and if possible a good explanation
in the code. Cause I wanna learn more/why it solved my problem and don't
want to copy & insert a code and be happy that it works.
Thanks in advance!
No comments:
Post a Comment