The accepted answer does not distinguish between the first launch and subsequent updates. Just setting logical in the general settings will only indicate if this is the first launch after the application was first installed. Later, if you want to update your application and make some changes to the first launch of this update, you will no longer be able to use this boolean, because the general settings are saved in updates.
This method uses general settings to save the version code, not the logical one.
private void checkFirstRun() { final String PREFS_NAME = "MyPrefsFile"; final String PREF_VERSION_CODE_KEY = "version_code"; final int DOESNT_EXIST = -1;
You will probably call this method from onCreate in your main action so that it checks every time your application starts.
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkFirstRun(); } private void checkFirstRun() {
If you need, you can configure the code to perform certain actions depending on which version the user has previously installed.
The idea came from this answer . They are also useful:
- How can you get the manifest version number from the XML variables of the application (Layout)?
- Custom version AndroidManifest.xml name value in code
Suragch May 16 '15 at 10:07 2015-05-16 10:07
source share