How can you know if it is the first time the app is started?
It would be interesting, in the event that the user updates the application, it is detected as new or update.
How can you know if it is the first time the app is started?
It would be interesting, in the event that the user updates the application, it is detected as new or update.
You can use Shared Preferences to save values persistently. Then you could save a variable containing the version name of the app and check it every time the app starts. Also, the Shared Preferences are maintained even after updating, so this method would also serve to detect the first execution after an update.
The idea would be that you have a constant with the version of the application (for example a String of the type "1.0.0"). And then follow an algorithm like this when starting the application:
If the value of the constant is different from the value of Shared Preferences from step 1, then this is the first time the app is starting:
Save the value of the constant with the version in the Shared Preferences .
Solved , taking ideas from the answers:
The values returned by the function:
Usage example:
Functionality improvements and code optimization are accepted.
EDITED
Another way is to use the Once library :
Initialize with:
Once.initialise(this);
First time methods:
The first time the user installs the app:
The first time after app upgrade:
UPDATED APRIL 2017
There is also the possibility of obtaining if it is an installation of 0 or an update, obtaining the values of the APK
SharedPreferences can be used , keep in mind that these values can be lost when you uninstall the app.
You can save using the method
setInicia()
, sending the value of 1 when it is the first installation and you can get the value of that preference using the methodgetInicia()
However, if you want to define the first time that the application starts on a device, the method (although it is also not completely infallible) that I use is to write a file containing the string "1", inside the directory
/Android/data
and not inside the directory of the app/Android/data/com.midominio.miapp
These are the methods used: