Activity lifecycle, saving to a database, and remembering row IDs

by Jesse McGrew » Thu, 05 Mar 2009 01:32:44 GMT

Sponsored Links
 I'm writing a simple app based on on the NotePad V3 example. In my
activity to edit a record, I have these methods:

    protected void onSaveInstanceState(Bundle outState) {
        if (mRowId != null)
                outState.putLong(MyDbAdapter.KEY_ROWID, mRowId);

    protected void onPause() {

saveState() is defined pretty much as it is in the example: if mRowId
is set, it updates the existing database row, otherwise it creates a
new row and sets mRowId to point to it.

The problem I'm having is that when I change the screen orientation
during this activity, I get duplicate rows. A row is inserted when I
flip the screen, then another row is inserted when I leave the

Presumably, onSaveInstanceState() is being called first when the
activity is destroyed for the configuration change. Since there's no
row ID yet, it doesn't store a row ID in the bundle. Then onPause() is
called, which creates a new row, but it's too late to save the row ID
for next time. When the activity is recreated in the new orientation,
the bundle has no row ID, so the activity thinks it's creating a new
record instead of editing the one it just saved.

What's the recommended fix here? Should I persist to the database in
onSaveInstanceState() as well as onPause()? Or is there some way for
onPause() to store the row ID where the activity can find it later
after being recreated?



Activity lifecycle, saving to a database, and remembering row IDs

by Jesse McGrew » Thu, 05 Mar 2009 13:05:56 GMT


FWIW, I ended up making it save the row in either onSaveInstanceState
() or onPause(), whichever one runs first.

I also looked at the notepad app in the SDK samples, which uses a
content provider. The way that one works is it creates the row as soon
as you start editing a note, and then deletes the row if you leave
without entering anything. I decided not to use that approach - I only
want valid rows in the database.


Sponsored Links

Other Threads

1. Nexus One for on Android Market Publisher Site


On Friday I read this announcement:

When I connect to my developer console and click on Developer Phones I
still see ADP2 only.

Does anybody see Nexus One there? What is its price?



2. Tabs + framelayout children


   I'm playing around with APIDemo's Views->Tabs->Content by Intent.
It seems that when I switched to a tab, there is a new additional
FrameLayout child which contains the tab content. So if I clicked on
Tab1, then it'll add one child to FrameLayout, then Tab2, adds the 2nd
child and so on. Say I have all 3 children in FrameLayout, how do i
know if I click on Tab1, which FrameLayout child does it corresponds



3. webkit, javascript, onclick, MotionEvent, event handlers not firing

4. Help, please - How to display videos as thumbs?

5. Need help - videos won't show as thumbs

6. OOT - Perkenalan

7. Touching the Liquid (Acer Smart Handheld "Liquid" Competition)