如何在方向更改时使用onSaveInstanceState和onRestoreInstanceState保存状态

时间:2012-04-21 14:27:36

标签: android android-orientation

我在Stack溢出中读了几乎所有关于onSaveInstanceState和onRestoreInstanceState的文章,但我无法解决我的问题。
我在main.java中有一个文本视图和按钮,当你点击按钮值( a 是一个int变量)时,变量将增加并显示在文本视图中,但是,当我旋转时我的手机(方向更改),文本视图重置。

我重写onSaveInstanceState和onRestoreInstanceState但它不起作用。 还有一件事,我有横向视图的特殊layout-land.xml文件。

这是我的代码

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

这里是我的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>

我真的需要一些帮助。 我非常感谢你的帮助。

5 个答案:

答案 0 :(得分:7)

作为更新,您只需设置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"/>

或者


这是最简单的例子:

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

但是当你开始在方向改变上保存很多对象时,拥有一个代表你的状态的内部类会更好。

使用内部类看起来如下所示。你可以想象,因为你有越来越多的状态来保存内部类使得处理程序变得更容易(不那么混乱)。

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

答案 1 :(得分:5)

我认为此代码对您有所帮助..

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

            }
        });
    }
}

答案 2 :(得分:0)

您需要使用保存的值重建视图组件。 使用保存的值填充文本视图。

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

答案 3 :(得分:0)

你真的想使用这些方法吗?

在这里,您可以找到解决问题的方法,只需将其放入清单文件

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

如果您这样做,当您更改方向时,您的活动将不会重新加载重置其值的所有视图

答案 4 :(得分:0)

一个被忽视但严重的错误。 您必须首先将数据传递给onSavedInstanceState()和调用onSavedInstanceState()。如果你认为那么你会发现在传递要保存的数据之前调用onSavedInstanceState是没用的。它不会保存活动实例,因为onSavedInstanceState()之前被调用.. 它应该是:

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) 
{           
    SavedInstanceState.putInt("salavat-count", a);
    super.onSaveInstanceState(SavedInstanceState); 
}