0

EULA dialog shows correctly until there is an application update. If the user has already accepted the EULA dialog it should not show again after the update.

public class AppEULA {
    private String EULA_PREFIX = "appeula";
    private Activity mContext;

    private static final String TAG = MainActivity.class.getSimpleName();

    public AppEULA(Activity context) {
        mContext = context;
    }

    private PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = mContext.getPackageManager().getPackageInfo(
                    mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }

    public void show() {
        PackageInfo versionInfo = null;
        try {
            versionInfo = mContext.getPackageManager()
                    .getPackageInfo(mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        // The eulaKey changes every time you increment the version number in
        // the AndroidManifest.xml
        final String eulaKey = EULA_PREFIX + versionInfo.versionCode;
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(mContext);

        boolean bAlreadyAccepted = prefs.getBoolean(eulaKey, false);
        if (bAlreadyAccepted == false) {
         ....
        show dialog


 .setPositiveButton(R.string.accept,
                            new Dialog.OnClickListener() {

                                @Override
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    // Mark this version as read.
                                    SharedPreferences.Editor editor = prefs
                                            .edit();
                                    editor.putBoolean(eulaKey, true);
                                    editor.commit();

                                    // Close dialog
                                    dialogInterface.dismiss();

I expect the EULA dialog not to show after application update if the user has already accepted it.

Gerasimos
  • 83
  • 8
  • 1
    What value do you get at bAlreadyAccepted (set a breakpoint there and have a look). Also, please provide the code where you're setting the bAlreadyAccepted – Nikos Hidalgo Oct 01 '19 at 13:09
  • If I already have accepted the EULA It returns true, else it's false.My problem is that even if I have accepted the EULA and change the version code bAlreadyAccepted returns false. boolean bAlreadyAccepted = prefs.getBoolean(eulaKey, false); this is where i set it. – Gerasimos Oct 01 '19 at 13:21
  • the keys in sharedpreferences are unique. You have an eulakey and you accept it and save it in shared preferences as true, but then you update the key, so when the check comes it doesn't find anything in your shared preferences under the new key and it returns the default value which is -as you set it- false. – Nikos Hidalgo Oct 01 '19 at 13:32
  • Ok, thanks alot.I will try to change the code and I will let you know. – Gerasimos Oct 02 '19 at 08:24

0 Answers0