As Android application developer sometimes when we release a new version of our application, we want to provide information to user about what changes have been made or what features have been added into the new version. The best way is to display a ‘What’s New‘ dialog that shows information about new changes and features when the application starts after updated. This dialog should only be displayed once when application starts. If user want to open the dialog manually, we can provide it via a menu.

To display a What’s New dialog, we can compare current application version number with the previous version number. If the current version number is greater then previous, that means the application is newer or recently updated then we should display the dialog and save the current version number .
Code implementation in Android:
public class MainActivity extends Activity {
private static final String PRIVATE_PREF = "myapp";
private static final String VERSION_KEY = "version_number";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
SharedPreferences sharedPref = getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE);
int currentVersionNumber = 0;
int savedVersionNumber = sharedPref.getInt(VERSION_KEY, 0);
try {
PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
currentVersionNumber = pi.versionCode;
} catch (Exception e) {}
if (currentVersionNumber > savedVersionNumber) {
showWhatsNewDialog();
Editor editor = sharedPref.edit();
editor.putInt(VERSION_KEY, currentVersionNumber);
editor.commit();
}
}
private void showWhatsNewDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_whatsnew, null);
Builder builder = new AlertDialog.Builder(this);
builder.setView(view).setTitle("Whats New")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
Look at init() method (line 14):
try {
PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
currentVersionNumber = pi.versionCode;
} catch (Exception e) {}
int savedVersionNumber = sharedPref.getInt(VERSION_KEY, 0);
if (currentVersionNumber > savedVersionNumber) {
showWhatsNewDialog();
Editor editor = sharedPref.edit();
editor.putInt(VERSION_KEY, currentVersionNumber);
editor.commit();
}
Related post:
Hello and thanks! I’ve a problem: no changelogs show me, I add this:
Toast.makeText(getApplicationContext(), “version number”+currentVersionNumber, Toast.LENGTH_SHORT).show();
after:
} catch (Exception e) {}
toast show me only 1, I’ve:
android:versionCode=”1″
android:versionName=”1.30″
I change 30 to 31 and I re-check but toast show me always 1
what’s the problem?
thanks!
hi
variable currentVersionNumber is type int. Plz change to float to get right current version.