简体   繁体   中英

Button text gets reset on orientation change

In my android app, I am trying to solve an issue with orientation change.

I have a main layout where I have two buttons. On click of the first button (default text on this button is "Select a category"), a dialog box appears with a category list with categories displayed as radio buttons. After the user selects a category, the selected category name appears on the button. Now when I change the orientation in the emulator, the Button text gets reset again. I have used onSaveInstanceState() like below.

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

I am able to get the category name back in onCreate(), the sysout prints correctly. But it is not getting set as the button text after change in orientation. Please let me know if I am doing anything wrong.

Thanks

Saving and restoring the data works using two Activity lifecycle methods called onSaveInstanceState() and onRestoreInstanceState().

To save the state information override onSaveInstanceState() method and add key-value pairs to the Bundle object that is saved in the event that your activity is destroyed unexpectedly. This method gets called before onStop().

To recover your saved state from the Bundle override onRestoreInstanceState() method. This is called after onStart() and before onResume(). Check the below code

             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);
}
 }
@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.
 }

To Retrieve the data

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

Add android:configChanges="orientation|screenSize" in your Android Manifest file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM