Is app first Launch Or Newer by checking the version on SharedPreferences - Android android.content

Android examples for android.content:SharedPreferences

Description

Is app first Launch Or Newer by checking the version on SharedPreferences

Demo Code

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;

public class Main {

  private static final String VERSION_CODE = "version_code";

  public static boolean firstLaunchOrNewer(Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    try {//from   ww w  . j  av a2 s .com
      int currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
      int lastVersion = sp.getInt(VERSION_CODE, -1);
      return currentVersion > lastVersion;
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      throw new IllegalArgumentException(e.getMessage());
    }

  }

}

Related Tutorials