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());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…