按钮文字会在方向更改时重置

时间:2018-08-30 05:47:07

标签: android onsaveinstancestate

在我的Android应用中,我正在尝试解决方向更改的问题。

我有一个主要布局,其中有两个按钮。单击第一个按钮(此按钮上的默认文本为“选择类别”)时,将出现一个对话框,其中包含类别列表,其中类别显示为单选按钮。用户选择类别后,所选类别名称将显示在按钮上。现在,当我在模拟器中更改方向时,Button文本将再次重置。 我已经使用过如下所示的onSaveInstanceState()。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// Initialization code

categoryList=(Button)findViewById(R.id.category_selection);

    if (savedInstanceState != null)
    {
        System.out.println("savedInstanceState--- 
        "+savedInstanceState.getString("bundle_category_name"));
        categoryName=savedInstanceState.getString("bundle_category_name");
        categoryList.setText(categoryName);
    }
    else
    {
        categoryList.setText(R.string.category);
    }
// remaining code 
}

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    // Save selected category name
    System.out.println("saving category name "+categoryName);
    outState.putString("bundle_category_name", categoryName);
}

我能够在onCreate()中获取类别名称,sysout可以正确打印。但是在方向改变后,它没有被设置为按钮文本。 如果我做错了任何事情,请告诉我。

谢谢

3 个答案:

答案 0 :(得分:0)

使用两个活动生命周期方法onSaveInstanceState()和onRestoreInstanceState()保存和还原数据。

要保存状态信息,请重写onSaveInstanceState()方法,并将键/值对添加到在意外破坏活动的情况下保存的Bundle对象中。此方法在onStop()之前调用。

要从Bundle重写onRestoreInstanceState()方法恢复保存的状态。在onStart()之后和onResume()之前调用此方法。检查以下代码

             public class MainActivity extends Activity{

       private static final String SELECTED_ITEM_POSITION = "ItemPosition";
       private int mPosition;

        @Override
      protected void onSaveInstanceState(final Bundle outState) {
    super.onSaveInstanceState(outState);

    // Save the state of item position
     outState.putInt(SELECTED_ITEM_POSITION, mPosition);   
}

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

    // Read the state of item position
    mPosition = savedInstanceState.gettInt(SELECTED_ITEM_POSITION);
}
 }

答案 1 :(得分:0)

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
 // Save UI state changes to the savedInstanceState.
 // This bundle will be passed to onCreate if the process is
// killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
    savedInstanceState.putString("MyString", "back to Android");
// etc.
 }

获取数据

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
 boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
 double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
  }

答案 2 :(得分:0)

在Android清单文件中添加android:configChanges =“ orientation | screenSize”。

 <activity android:name="YourActivity"
  ...
  android:configChanges="orientation|screenSize"
  .../>