Şahin Usif
Şahin Usif

Reputation: 11

Android: How to execute some code only when I update the app on market?

I need to know if there's a way to run a piece of code in my app only first time when the user installs my new updates from market(google play) ?

I need to run the code once each time I upload a new version of my app ?

Thanks for reading.

Upvotes: 0

Views: 2170

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82553

Use SharedPreferences to store the last version and the current version of your app, and run the code once on every version change. Something like:

    public static final String LAST_VERSION = "LAST_VERSION";
    public static final int VERSION_CODE = 4; // Same as the version code in your manifest.

    SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();

    mContext = this;

    int last = prefs.getInt(LAST_VERSION, 0);
    if (last < VERSION_CODE) {
        //Your code
        editor.putInt(LAST_VERSION, VERSION_CODE);
        editor.commit();
    }

Upvotes: 7

Related Questions