How To Save State With Onsaveinstancestate And Onrestoreinstancestate While Orientation Change
Solution 1:
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;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yourTextView = (TextView) findViewById(R.id.your_textview);
}
@OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
outState.putString("YourTextViewTextIdentifier", yourTextView.getText().toString());
super.onSaveInstanceState(outState);
}
@OverrideprotectedvoidonRestoreInstanceState(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.
privatestaticclassStateimplementsSerializable {
privatestatic final StringSTATE = "com.your.package.classname.STATE";
privateString yourTextViewText;
publicState(String yourTextViewText) {
this.yourTextViewText = yourTextViewText;
}
publicStringgetYourTextViewText() {
return yourTextViewText;
}
}
@OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
State s = newState(yourTextView.getText().toString());
outState.putSerializable(State.STATE, s);
super.onSaveInstanceState(outState);
}
@OverrideprotectedvoidonRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
State s = (State) savedInstanceState.getSerializable(State.STATE);
yourTextView.setText(s.getYourTextViewText());
}
Solution 2:
I think this code is helpful for you..
publicclassMainActivityextendsActivity
{
protectedvoidonSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
System.out.println("TAG, onSavedInstanceState");
finalTextViewtext= (TextView)findViewById(R.id.textView1);
CharSequenceuserText= text.getText();
outState.putCharSequence("savedText", userText);
}
protectedvoidonRestoreInstanceState(Bundle savedState)
{
System.out.println("TAG, onRestoreInstanceState");
finalTextViewtext= (TextView)findViewById(R.id.textView1);
CharSequenceuserText= savedState.getCharSequence("savedText");
text.setText(userText);
}
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalStringname="5";
finalTextViewshow= (TextView)findViewById(R.id.textView1);
Buttonbtn= (Button)findViewById(R.id.button1);
btn.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(View v)
{
// TODO Auto-generated method stub
show.setText(name);
}
});
}
}
Solution 3:
you need to reconstruct your view components with saved values. fill your textview with the saved value.
@OverrideprotectedvoidonRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
a = savedInstanceState.getInt ("salavat-count");
tv.setText(""+a);
}
Solution 4:
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
<activityandroid: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
Solution 5:
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 :
@OverrideprotectedvoidonSaveInstanceState(Bundle SavedInstanceState)
{
SavedInstanceState.putInt("salavat-count", a);
super.onSaveInstanceState(SavedInstanceState);
}
Post a Comment for "How To Save State With Onsaveinstancestate And Onrestoreinstancestate While Orientation Change"