how to save state with onSaveInstanceState and onRestoreInstanceState while orientation change

爱⌒轻易说出口 提交于 2019-11-29 08:12:48
Durgpal Singh

I think this code is helpful for you..

public class MainActivity extends Activity 
{

    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        System.out.println("TAG, onSavedInstanceState");

        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = text.getText();
        outState.putCharSequence("savedText", userText);
    }
    protected void onRestoreInstanceState(Bundle savedState) 
    {       
        System.out.println("TAG, onRestoreInstanceState");
        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = savedState.getCharSequence("savedText");
        text.setText(userText);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String name = "5";

        final TextView show = (TextView)findViewById(R.id.textView1);
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() 
        {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                show.setText(name);

            }
        });
    }
}

As an update, you could just set freezesText="true"

http://developer.android.com/reference/android/R.attr.html#freezesText

 <TextView
    android:id="@+id/eltemas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:freezesText="true"/>

Or


This is the simplest example:

TextView yourTextView;

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

    yourTextView = (TextView) findViewById(R.id.your_textview);

}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("YourTextViewTextIdentifier", yourTextView.getText().toString());

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        yourTextView.setText(savedInstanceState.getString("YourTextViewTextIdentifier"));
    }

But having an inner class that represents your state will be much nicer when you start saving a lot of objects on orientation change.

Using an inner class it would look like this below. You can imagine as you have more and more state to save the inner class makes it much easier (less messy) to handler.

  private static class State implements Serializable {

    private static final String STATE = "com.your.package.classname.STATE";

    private String yourTextViewText;

    public State(String yourTextViewText) {
        this.yourTextViewText = yourTextViewText;
    }

    public String getYourTextViewText() {
        return yourTextViewText;
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    State s = new State(yourTextView.getText().toString());

    outState.putSerializable(State.STATE, s);

    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    State s = (State) savedInstanceState.getSerializable(State.STATE);

    yourTextView.setText(s.getYourTextViewText());
}

you need to reconstruct your view components with saved values. fill your textview with the saved value.

@Override   
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  a = savedInstanceState.getInt ("salavat-count");
  tv.setText(""+a);
}

Do you really want to use these methods ?

Here you go a kind of solution to your problem, just put this inside your manifest file

<activity android:name="package.subpackage.xptoactivity" android:configChanges="orientation"></activity>

If you do this, your Activity won't reload all the views reseting their values when you change the orientation

DUDE_MXP

A neglected but serious mistake. You have to first pass the data to onSavedInstanceState() and the call onSavedInstanceState() . If you think then you will find that its useless to call onSavedInstanceState before passing the data to be saved. it wont save the activity instance as onSavedInstanceState ()is called before.. It should be as :

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) 
{           
    SavedInstanceState.putInt("salavat-count", a);
    super.onSaveInstanceState(SavedInstanceState); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!