In the android application I want to implement the appearance of a pop-up window with information about changes in the new version of the application.

If the user is updating the application, I want to display a pop-up window.

For the initial installation with GooglePlay, this window should not appear.

How can you distinguish programmatically the initial installation of the application and the update?

    3 answers 3

    There is such an approach - register the receiver to reinstall your application:

    <receiver android:name=".MyAppUpdatedReceiver"> <intent-filter> <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/> </intent-filter> </receiver> 

     public class MyAppUpdatedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // этот код будет выполнен после каждой переустановки // но при первой установке не вызывается } } 

    Another option: write somewhere the version number and with each start compare with the current one, if changed, we display the changelog

    • I tried the other option initially (I wrote it in a separate file, and tried SharedPreferences ), but I had to SharedPreferences it because of the features of the application, although it is also working. - V. March
    • But BroadcastReceiver works regardless of the entry point into the application. - V. March

    You can check this:

     public static boolean isFirstInstall() { try { long firstInstallTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).firstInstallTime; long lastUpdateTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime; return firstInstallTime == lastUpdateTime; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return true; } } public static boolean isInstallFromUpdate() { try { long firstInstallTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).firstInstallTime; long lastUpdateTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime; return firstInstallTime != lastUpdateTime; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } } 
    • For some reason, the second method works every time the application is opened. - V.March

    Everything is very simple.

    In theory:

    You get the programmatically current version of the application code, save it to SharedPrefereneces , and make a condition that if the saved version matches the current version, nothing happens. That is, the user first downloaded the application, the code version of which is 1, or even 32, and this number is saved. And as soon as the saved code number becomes less than the current one, it will mean that the application has been updated, then the condition of opening a dialog box, for example, with a list of changes, is satisfied.

    On practice:

     // Реализовываем SharedPreferences SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(this); @SuppressLint("CommitPrefEdits") SharedPreferences.Editor mEditor = mPreferences.edit(); // Получаем текущую версию кода int versionCode = BuildConfig.VERSION_CODE; // Получаем сохраненную версию кода int lastVersionCode = mPreferences.getInt("update_true", versionCode); // Если lastVersionCode меньше чем текущий versionCode выполняется условие if (lastVersionCode < versionCode) { // Вызываем диалоговое окно со списком измненений } // Сохраняем текущую версию кода mEditor.putInt("update_true", versionCode).apply(); 
    • Good idea, thanks for describing the code in detail. He works. The only thing is that I have several entry points to the application from which the user comes in (costs of distributing access levels), so in this case it’s not quite convenient to hang up checks for several activations. I had to look for an alternative. - V. March