Use app version to show a dialog only after update

I decided to enhance my Android app so that it will show a little dialog with the newest features after starting.

If this dialog would show up after every start it would be very annoying.

The goal is to show it only when the user has just installed the app for the first time or after the app was upgraded to a newer version.

The solution is very simple:

  1. Get the current version of the app.
  2. Compare with the last version that the user has accepted.
  3. Show dialog if current version is newer.
  4. Store current version as accepted.

Storing the accepted version in the SharedPreferences is very easy.

Getting the current version of the app is also easy, but it can be difficult to find out how to do it.
The API is somewhat hidden – you ask the package manager to give the PackageInfo and there you can access the current versionName.

Here is a snippet of the code from the onCreate() in my main activity.

	try {
		PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
		String currentVersion = packageInfo.versionName;
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		String acceptedVersion = sharedPreferences.getString("pref_acceptedVersion", "");
		if (currentVersion.compareTo(acceptedVersion) > 0) {
			Editor edit = sharedPreferences.edit();
			edit.putString("pref_acceptedVersion", currentVersion);
			showMessage(this, getResources().getString(R.string.new_version_message, currentVersion));
	} catch (NameNotFoundException e) {

showMessage() is a simple static method that shows a dialog.

	private static void showMessage(Context context, int messageId) {
		showMessage(context, context.getResources().getString(messageId));
	private static void showMessage(Context context, String message) {
		new AlertDialog.Builder(context)
		.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
This entry was posted in Android, Java, Tips and Tricks. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *