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. Console logging missing on Eclipse

I'm running Eclipse Ganymede on Ubuntu 8.04 and suddenly with no 
configuration changes as of yesterday am missing logging on the Console 
Window.  I see startup logging when I launch an app but once launched 
there are no more lines of information.  The buttons for Verbose etc are 
also missing.  Log.v messages are not showing.  Anyone else seeing 
this?  I'm wondering if one of Ubuntu's updates screwed things up?  I 
may try reinstalling the plug-in or even Eclipse but thought I would 
check here first.

Brian Conrad


2. system crash dump....when a phone freezes

If a phone freezes, we want to know how it got frozen ?? How to get
the order to know the reason for crash..?? how to get system
crash dumps..??


3. How to find out screen status: active/dark?

4. can not find a file in dynamically loaded jar

5. SpriteText and coloured surfaces

6. Parsing Dates in other Locales

7. Help with Services