How to work with terms and conditions in an Android application in Android Studio

I developed the application, and I want my first page to be similar to the action of conditions in which the action is displayed only for the first time after installation. And then, if the user accepts it, he will never be displayed on this device again. If the user rejects the condition, it will be displayed every time the application starts.

My question is: "How can I implement the code?"

+3
source share
1 answer

Show it in AlertDialogand put the result (Accept or Decline) in the value SharedPreferencesas boolean. Then just check the key value every time you launch your application.

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean agreed = sharedPreferences.getBoolean("agreed",false);
if (!agreed) {
    new AlertDialog.Builder(context)
        .setTitle("License agreement")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Editor editor = sharedPreferences.edit();
                editor.putBoolean("agreed", true);
                editor.commit();
            }
        })
        .setNegativeButton("No", null)
        .setMessage(text)
        .show();
}
+4

All Articles