Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Perform a task while application opened for first time alone?

Tags:

java

android

I need to create database table when application runs first time after installing. So how to get the status of the application running first time when app is installed? I have heard about SharedPreferences but not familiar with it. Any code help is appreciated and thanks in advance...

like image 581
Pattabi Raman Avatar asked Jun 07 '26 07:06

Pattabi Raman


2 Answers

SQLiteOpenHelper has a onCreate method which gets called if the database does not exist and needs to be created for the first time.

Use this to create and initialise your database with whatever data you need in the tables.

like image 171
zaf Avatar answered Jun 08 '26 21:06

zaf


if you are trying to insert a value in the database on the application first run you can put a value in the sharedPref like this :

    private static void SaveBooleanPreferences(String key, boolean value, Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();

    }
    private static boolean getBooleanPreferences(String key, Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME,0);
        return sharedPreferences.getBoolean(key, false);

    }

after you detecting the value of the first run try to insert into the database, Android OS will handel the creation of the database for you there are no need to create it by your self !

like image 27
daigoor Avatar answered Jun 08 '26 20:06

daigoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!