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:

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

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

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
activity.

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?

Jesse

--~--~---------~--~----~------------~-------~--~----~



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.

Jesse
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. Nexus One for on Android Market Publisher Site

Hello,

On Friday I read this announcement:
http://googlenexusoneboard.blogspot.com/2010/07/update-nexus-one-changes-in.html.

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?

Thanks
Tom

-- 

2. Tabs + framelayout children

Hi,

   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
to?

Thanks,
-Tim

-- 

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)