Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

android - how to save state with onSaveInstanceState and onRestoreInstanceState while orientation change

I read almost all article about onSaveInstanceState and onRestoreInstanceState in Stack overflow but I cant solve my problem.
I have a text view and button in my main.java and while you click on button value of a ( a is an int variable) variable will increase and show in the text view, but, when I rotate my phone ( orientation change), text view reset.

I override onSaveInstanceState and onRestoreInstanceState but it doesn't work. one more thing, I have special layout-land.xml file for landscape view.

here is my code

 import android.app.Activity; 
 import android.graphics.Typeface; 
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

 public class main extends Activity {
     /** Called when the activity is first created. */   
   public int a = 0;     
   public String fonts="TAHOMA.TTF";

   TextView tv = (TextView) findViewById(R.id.salavat);      
   Button b = (Button) findViewById(R.id.showsalavat);

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       b.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
               a++;
               tv.setText(""+a);
            }
       });
   }
   @Override        
   protected void onSaveInstanceState(Bundle SavedInstanceState) {
      super.onSaveInstanceState(SavedInstanceState);            
      SavedInstanceState.putInt("salavat-count", a);
   }
   @Override    
   protected void onRestoreInstanceState(Bundle savedInstanceState) {
       super.onRestoreInstanceState(savedInstanceState);     
       a= savedInstanceState.getInt ("salavat-count");   
   } 
}

and here my main.xml

    <TextView
        android:id="@+id/shoma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/shoma"
        android:textSize="20sp" />

    <Button
        android:id="@+id/showsalavat"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:text="+"
        android:textSize="100sp" />

    <TextView
        android:id="@+id/eltemas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="55dp"
        android:text="@string/eltemas" />

    <TextView
        android:id="@+id/salavat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="35sp" />

    <TextView
        android:id="@+id/ferestade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:text="@string/salavat"
        android:textSize="20sp" />

</LinearLayout>

I really need some help on this. I really appreciate your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...