Hi,
I feel your pain.  The life cycle of an activity seemed strange to me
until I began to log every entrance into these methods.  As a result I
began to understand what was going on and why.

I can't seem to find a way to attach this code to the reply so I'll
have to just insert it here.

This works very well for me on my Archos 43IT (Android 2.2.1).  You
just need to follow this simple logic replacing it with your own
requirements.  This is a simple new user info panel.

/**
 * OnSaveInstanceState()
 *
 * Called to retrieve per-instance state from an activity before being
killed so that the state can be restored in onCreate(Bundle) or
 * onRestoreInstanceState(Bundle) (the Bundle populated by this method
will be passed to both).
 */
@Override
public void onSaveInstanceState (Bundle outState) {
        Log.i("MyApp", "MyApp.MyActivity : Processing
onSaveInstanceState...");
        super.onSaveInstanceState(outState);

        // Get New User Info panel
        RelativeLayout info =
(RelativeLayout)findViewById(R.id.new_user_info_layout);

        // Test application state
        if (info != null && info.isShown()) {

            // Save first name
            EditText text =
(EditText)findViewById(R.id.createFirstNameEditText);
            outState.putParcelable("firstName",
text.onSaveInstanceState());

            // Save last name
            text =
(EditText)findViewById(R.id.createLastNameEditText);
            outState.putParcelable("lastName",
text.onSaveInstanceState());

            // Save handle
            text = (EditText) findViewById(R.id.createHandleEditText);
            outState.putParcelable("handle",
text.onSaveInstanceState());

            // Save Birthday day
            DatePicker datePicker =
(DatePicker)findViewById(R.id.createBirthdayDatePicker);
            outState.putInt("birthDay", datePicker.getDayOfMonth());

            // Save Birthday month
           outState.putInt("brthMonth", datePicker.getMonth());

           // Save Birthday year
           outState.putInt("birthYear", datePicker.getYear());

            // Save Gender
            RadioGroup radioGroup =
(RadioGroup)findViewById(R.id.createGenderRadioGroup);
            outState.putInt("gender",
radioGroup.getCheckedRadioButtonId());

            // Save Note
            text = (EditText)findViewById(R.id.createNoteEditText);
            outState.putParcelable("note",
text.onSaveInstanceState());
        }
}


/**
 * onRestoreInstanceState ()
 *
 * This method is called after onStart() when the activity is being re-
initialized from a previously saved state, given here in
savedInstanceState.
 * Most implementations will simply use onCreate(Bundle) to restore
their state, but it is sometimes convenient to do it here after all of
the
 * initialization has been done or to allow subclasses to decide
whether to use your default implementation. The default implementation
of this
 * method performs a restore of any view state that had previously
been frozen by onSaveInstanceState(Bundle).
 */
@Override
public void onRestoreInstanceState  (Bundle savedInstanceState) {
        Log.i("MyApp", "MyApp.MyActivity : Processing
onRestoreInstanceState...");
        super.onRestoreInstanceState (savedInstanceState);

        // Get New User Info panel
        RelativeLayout info =
(RelativeLayout)findViewById(R.id.new_user_info_layout);

        // Test application state
        if (info != null && info.isShown()) {

            // Restore first name
            EditText text =
(EditText)findViewById(R.id.createFirstNameEditText);
 
text.onRestoreInstanceState(savedInstanceState.getParcelable("firstName"));

            // Restore last name
            text = (EditText)findViewById(R.id.createLastNameEditText);
 
text.onRestoreInstanceState(savedInstanceState.getParcelable("lastName"));

            // Restore handle
            text = (EditText) findViewById(R.id.createHandleEditText);
 
text.onRestoreInstanceState(savedInstanceState.getParcelable("handle"));

            // Restore Birthday year, month, day
            DatePicker datePicker =
(DatePicker)findViewById(R.id.createBirthdayDatePicker);
            datePicker.updateDate(savedInstanceState.getInt("birthYear"),
savedInstanceState.getInt("birthMonth"),
savedInstanceState.getInt("birthDay"));

            // Restore Gender
            RadioGroup radioGroup =
(RadioGroup)findViewById(R.id.createGenderRadioGroup);
            radioGroup.check(savedInstanceState.getInt("gender"));

            // Restore Note
            text = (EditText)findViewById(R.id.createNoteEditText);
 
text.onRestoreInstanceState(savedInstanceState.getParcelable("note"));
        }
}


You must override these to methods in order to implement the
recommended run time state persistence, although there are also other
ways.  Also you'll notice that I place the log statements "BEFORE" the
call to super() even though it is not recommended to do this.  I do it
because some of the life cycle activities will transition immediately
and then return making it difficult to clearly demarcate when each of
the life cycle methods begins.  Try it for yourself and see.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to