views:

53

answers:

1

I'm trying to create a "what's new" pop-up dialog when users update (or, I guess, install) my app. I have the following bit of logic to check that the last installed version was then compare it to the current version, then save the current version as the "last" version (this is called at the end of the onCreate() of my "first" Activity):

    int lastVersion = getPreferences(MODE_PRIVATE).getInt(LAST_VERSION, 34);
    try {
        int currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

        Log.d(getClass().getSimpleName(), "Old Version = " + lastVersion);
        Log.d(getClass().getSimpleName(), "New Version = " + currentVersion);

        if (currentVersion != lastVersion) {
            // write the current version to the preferences so they won't see the popup again
            getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

            // tell the user the new thing
            AlertDialog dia = new AlertDialog.Builder(this)
                .setTitle(R.string.main_menu_new_title)
                .setMessage(R.string.main_menu_new_body)
                .create();
            dia.show();
        }
    } catch (NameNotFoundException ex) {}        

The problem is that no matter how many times I run the app I always get the default value for lastVersion, so the pop-up ALWAYS happens. Ideas?

+2  A: 

You're writing the old version number back to the preferences.

Change:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, lastVersion)
                .commit();

to:

getPreferences(MODE_PRIVATE)
                .edit()
                .putInt(LAST_VERSION, currentVersion)
                .commit();
Dave Webb
Oh jeeze... late night. Thanks.
fiXedd