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. Adding data to contacts

I am building an app that extends the functionality of contacts, I
have pulled the contact data, but need to add data associated with
each record.  So you would have a user and their normal data stored in
contacts, but through this app it would pull all your contacts when
you click on the person you would get more data about them.  I assume
I have to make a custom ContentProvider.


2. Linking to the Amazon MP3 app

has anyone seen a web link which points to the Android version of the
Amazon MP3 store?

I see that the Shazam app links to the Amazon MP3 store, but I'm
looking for an example hyperlink from a web app.



3. SimpleCursorAdapter setAdapter not rendering any items

4. Mobile Dev Camp on November 29 and 30 in Amsterdam

5. is it possible to change the color of the stars of a RatingBar?

6. AudioFlinger routing configurations..

7. Android Has a Bright Future.. But What Does This Mean for the G1?