Hello,

in my application, I sometimes start other activities which use the
"Theme.Dialog" style the to show them like dialogs, as recommended in
the FAQ of the Android documentation. And up to now I overrided the
onSaveInstanceState and onRestoreInstanceState methods in my activity
to store / restore my Activity's state when configuration changes
occur, as shown in the example below.

Now I found out that there is a problem with that:
* When the other activity ("ActivityB" in my example) is started,
ActivityA's onSaveInstanceState is called (why?).
* While changing the screen orientation when ActivityB is shown in the
foreground and ActivityA in the background, onSaveInstanceState and
onRestoreInstanceState are not called for ActivityA.

In my example, this means that the content of the textbox of ActivityA
is properly restored when ActivityB is shown in the foreground only on
the first screen orientation change, but not when changing the
orientation for a second time.

Using the "savedInstanceState" bundle I get in my onCreate method
instead of using onRestoreInstanceState does not solve the problem for
me, because the stored values in the bundle get lost in the scenario I
described above.

What is wrong with the approach I chose?

--------------------------------------------------------------

package test.activitysave;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ActivityA extends Activity {
  private EditText txbTest;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    txbTest = new EditText(this);
    layout.addView(txbTest,
        new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

    Button button = new Button(this);
    button.setText("Start activity...");
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(ActivityA.this, ActivityB.class);
        ActivityA.this.startActivity(intent);
      }
    });
    layout.addView(button,
        new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

    setContentView(layout);
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    this.txbTest.setText(savedInstanceState.getCharSequence
("ENTERED_TEXT"));
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putCharSequence("ENTERED_TEXT", this.txbTest.getText());
  }
}

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

Reply via email to