Reputation: 157
In my app I show this dialog on startup. After clicking 'agree', I never want to show the dialog again. After clicking 'disagree', I want to close the app + show the dialog again when the app is relaunched. How can this be done programmatically?
This is my current code in Java, but I want it to be in Kotlin by the way.
new AlertDialog.Builder(this)
.setTitle("Terms of service")
.setMessage("This app will collect information to personalize ads.")
.setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// never show dialog again
}
})
.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close app, and show dialog when the app is opened again
}
})
.setIcon(R.drawable.ic_action_name)
.setCancelable(false)
.show();
Upvotes: 0
Views: 343
Reputation: 3730
SharedPreferences sharedPreferences = getSharedPreferences("prefs",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
new AlertDialog.Builder(this)
.setTitle("Terms of service")
.setMessage("This app will collect information to personalize ads.")
.setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); /// here you save a boolean value ,
// if the user agreed , check if true next app start and ignore the dialog
editor.putBoolean("agreed",true);
editor.apply();
}
})
.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
editor.putBoolean("agreed",false);
editor.apply();
finish();
}
})
.setIcon(R.drawable.ic_action_name)
.setCancelable(false)
.show();
SharedPreferences sharedPreferences = getSharedPreferences("prefs",MODE_PRIVATE);
boolean isAgreed = sharedPreferences.getBoolean("agreed",false);
if(isAgreed){
/// here ingore the dialog
}else {
/// show the dialog again
}
Upvotes: 1
Reputation: 2874
Write the value AGREE
/DISAGREE
to a file ./myAppName.properties
.. on startup check for the presence of the file.. if its present, read in the key=value pairs. If the value of your key is AGREE
do not show the dialog.. if its DISAGREE
/missing, present the user with a dialog to make a selection. How you do that is up to you - Swing/Command line, etc.
Upvotes: 0