1

I have two Activities, namely MainActivity and Favorites. On the MainActivity, I have something like this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  //function to insert data to database
  insertData();
}

and I have a link on the MainActivity to link to Favorites Activity. But when clicking the Up Navigation Button from the Favorites back to the MainActivity, the app was closing.

Is it ideal to put the insertion of data to onCreate event?

Please help. Thank you.

Here is the log:

11-08 15:50:14.848: E/SQLiteLog(8746): (5) database is locked
11-08 15:50:14.865: E/SQLiteDatabase(8746): Failed to open database '/data/data/example.pangasinantranslator/databases/dbTrans'.
11-08 15:50:14.865: E/SQLiteDatabase(8746): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:627)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:313)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:287)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:884)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MySQLiteHelper.addWord(MySQLiteHelper.java:46)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MainActivity.readFile(MainActivity.java:240)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MainActivity.onCreate(MainActivity.java:56)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.Activity.performCreate(Activity.java:5020)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.access$600(ActivityThread.java:181)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.os.Looper.loop(Looper.java:153)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.main(ActivityThread.java:5042)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at java.lang.reflect.Method.invoke(Method.java:511)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at dalvik.system.NativeStart.main(Native Method)
11-08 15:50:14.865: D/Here is error(8746): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
11-08 15:50:14.865: D/Insert:(8746): Inserting ..
ajdeguzman
  • 1,223
  • 3
  • 16
  • 27
  • No there is no problem to insert data to `onCreate` ,but if you are finish your MainActivity after calling Favorites than when you back press your app should be close...may be this happen in your problem.. – Abhishek Patel Nov 08 '13 at 07:17
  • 1
    Add the Log so that we know what originally are you facing. – Quamber Ali Nov 08 '13 at 07:21
  • @NSQuamber.java on the Log, it says `Database is Locked` – ajdeguzman Nov 08 '13 at 07:31
  • as per log i beleive you are using the database connection somewhere else or you might not have closed it – Quamber Ali Nov 08 '13 at 07:39
  • you mean it is okay to put the insertion of data inside the `onCreate` event? – ajdeguzman Nov 08 '13 at 07:40
  • 1
    First thing.. If you know what you have to insert( as i don't see any data passed) then add it to DatabaseHelper's onCreate() Method secondly if you are adding the data then check the database if you ahve got a readable database then add the data – Quamber Ali Nov 08 '13 at 07:41
  • also that i am not in favor of Activity's OnCreate Insertion – Quamber Ali Nov 08 '13 at 07:41
  • @NSQuamber.java where or when is the best time that I can close the database connection because i'm using it everytime I'm on the `MainActivity` and on the `Favorites` – ajdeguzman Nov 08 '13 at 07:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40802/discussion-between-nsquamber-java-and-ajdeguzman) – Quamber Ali Nov 08 '13 at 08:06
  • You can read up on an Activity life cycles [here](http://developer.android.com/training/basics/activity-lifecycle/starting.html). – GreekOphion Nov 08 '13 at 07:16

1 Answers1

2

Anything that you may need to do before the screen is shown to the user should be done in onCreate() before setContentView().

mmBs
  • 8,421
  • 6
  • 38
  • 46
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39