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. forde to open the Launcher, not the last Activity

I would like to force my app to go to the launcher when i clik on it
in the Home Screen

I am in any activity of my app and i click HOME button and go to the
Home Screen, If i click on my launcher then i always go to the last
activity where i clicked HOME button, instead of my launcher activity

I have tried with launch modes, with android:clearTaskOnLaunch, with
android:finishOnTaskLaunch and they never work!!!

Android always starts with the lastActivity onRestart() method instead
of my launcher activity.

How can i do that please? I need your help:

this is my code in the manifest:

<activity android:name=".cem.activity.LanzadorCeM"
                        android:label="@string/app_cem_name" 
android:icon="@drawable/
aprocor3">
                        <intent-filter android:label="@string/textLanzadorCeM">
                                <action 
android:name="android.intent.action.MAIN" />
                                <category 
android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                </activity>

-- 

2. totally 2d

Let's say you are making a totally 2d game for Android.  Does it make
sense to use OpenGL?  Will you get a better framerate or anything
besides

complexity?

Thanks.

-- 

3. Secure SDCard (MicroSD)

4. Well displayed in portrait but not in landscape

5. Amazon Market Reporting

6. Changing ImageView's image

7. CTS and frameword modifications