'How to call a specific function when the app is updated from Store or is First installed in Android?
I have an app with a Login functionality and also auto Login, and before login screen, a Splash Screen Activity is displayed. Inside Splash Screen activity I want to check if the user has just updated the app from the Google play to a newer version, and if so, to always show him the Login screen. So my question is, how to check if current version app is different from the last version? I tried solutions found in this topic: Detect if new install or updated version (Android app)
But in this function
public static boolean isInstallFromUpdate(Context context) {
try {
long firstInstallTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
long lastUpdateTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).lastUpdateTime;
return firstInstallTime != lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
This line is always true: firstInstallTime != lastUpdateTime;, but after an update, if I close the app and then reopen it, this line should be false. If you have any suggestions or need more details, please let me know! Thanks
Solution 1:[1]
I think a simple solution is if you have an admin panel of mobile app for data calling then you have to store version detail latest and these data related API call into your app so easily detect if you have to change the version in admin panel then show a dialog of update app and redirect to play store...
This is my personal opinion because I have used this kind of method .....
Solution 2:[2]
You can simply set a SharedPreference, if you open the app after an update. After you closed the app and reopened it, you should check for your isInstallFromUpdate(...) and the just set SharedPreference
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | axar |
| Solution 2 | Camillo Carmesin |
