Insert data in sqlite db when the activity is called first Time,stop inserting when return to the activity again or call the activity again Insert data in sqlite db when the activity is called first Time,stop inserting when return to the activity again or call the activity again sqlite sqlite

Insert data in sqlite db when the activity is called first Time,stop inserting when return to the activity again or call the activity again


how to data insert in sqlite database when first time activity call

Use SharedPreferences for executing database insert operation only once when application is launched first time:

SharedPreferences prefs = PreferenceManager.                       getDefaultSharedPreferences(getApplicationContext());if(!prefs.contains("insertedInDB")){   // insert in DB  // create key in prefs   SharedPreferences.Editor editor = prefs.edit();   editor.putBoolean("insertedInDB", true);   editor.commit();}else{   // no need to insert in db}


This is because you are entering it every time, the activity is created. The solution to ommit doulbe entries would be, to check wether this entry already exists in the database.

You could save in SharedPreferences a value, that you already inserted this entries to the database. You can imagine SharedPreferences like a table, which is very quick accessable and easy to use. To check it out, you could follow the Android Developer Guide here.

But you should think, if it is really necessary, that everytime the activity is created the same entries get inserted.