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. 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
JyotishTools.com


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

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

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

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-kernel+unsubscr...@googlegroups.com
website: 

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