2

Suppose that the app check if the start is her first run, with the following code:

public class MyActivity extends Activity {

SharedPreferences prefs = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Doing things

    prefs = getSharedPreferences("com.example.myAppName", MODE_PRIVATE);
}

@Override
protected void onResume() {
    super.onResume();

    if (prefs.getBoolean("firstrun", true)) {
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs
        prefs.edit().putBoolean("firstrun", false).commit();
    }
}}

Suppose now that this application is updated through Google Play, when the app runs again, that code will consider it his first start? Sahred Preferences are deleted in the update process?

Thank you for your time,

Flagg.

flagg327
  • 967
  • 1
  • 10
  • 21

3 Answers3

1

I would store the version of your app in sharedPreferences (instead of the boolean value). Then compare the last stored with your current stored.

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String currentVersion = pInfo.versionName;
if (prefs.getString("lastVersion", "0").equals(currentVersion)) {
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs
        prefs.edit().putString("lastVersion", currentVersion).apply();
    }

see also

Community
  • 1
  • 1
AlbAtNf
  • 3,859
  • 3
  • 25
  • 29
1

Your SharedPreferences will be retained through an app update. So you should be able to get your last stored value with the new version.

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
1

The answer to your question; Yes SharedPreference's persist through an update. They will only be removed if a user uninstalls the app or if you remove them.

vguzzi
  • 2,420
  • 2
  • 15
  • 19