Last Updated: February 25, 2016
·
3.995K
· iostreamer

Checking if App is used for the first time in 3 lines of code

File file=new File("path");
boolean exists=!file.createNewFile();
if(exists==false)
//do something

the point is to check if an empty file exists, if it doesn't then, yes, your app is running for the
first time

3 Responses
Add your response

Pretty Cool, but i recommend using SharedPreferences

SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
if (preferences.getBoolean("firstLogin", true)) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("firstLogin", false);
        editor.commit();
}

References: http://developer.android.com/reference/android/content/SharedPreferences.html

if you use the form, you'll have to give write permission to the file system if it is not used elsewhere, will be an unnecessary permission

over 1 year ago ·

Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.

http://developer.android.com/guide/topics/data/data-storage.html

over 1 year ago ·

true that, I used this only because I was already doing file handling. If file handling isn't involved in one's app then this thing is pretty much useless

over 1 year ago ·