Инструменты пользователя

Инструменты сайта


android:activity:save_instance

Сохранение параметров при вращении экрана

При вращении экрана в андроиде активити создаётся заново. Чтобы можно было вернуться к предыдущему состоянию - предусмотрены методы:

@Override
public    void onSaveInstanceState(Bundle savedInstanceState)
  // EditTexts text
  savedInstanceState.putString("first_et",((EditText)findViewById(R.id.et_first)).getText().toString());
  // EditTexts text
  savedInstanceState.putInt("first_et_v", ((EditText)findViewById(R.id.et_first)).getVisibility());
  super.onSaveInstanceState(savedInstanceState);
}
 
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
  super.onRestoreInstanceState(savedInstanceState);
  // EditTexts text
  ((EditText)findViewById(R.id.et_first)).setText(savedInstanceState.getString("first_et"));
  // EditText visibility
  ((EditText)findViewById(R.id.et_first)).setVisibility(savedInstanceState.getInt("first_et_v"));
}

Бывает некоторые элементы экрана падают с ексепшином

IllegalArgumentException: Wrong state class

У меня такое было для ListView, помогло android:saveEnabled выставление в false у списка

Что-то ещё про RadioButton писали - может тоже пригодится:

<RadioButton
    ...
    android:saveEnabled="false" />

http://stackoverflow.com/questions/6667603/illegalargumentexception-wrong-state-class

Для TextView можно использовать:

<TextView 
     ... 
     android:freezesText="true" />

http://stackoverflow.com/questions/5179686/restoring-state-of-textview-after-screen-rotation

/var/www/source/data/pages/android/activity/save_instance.txt · Последнее изменение: 2024/02/05 12:40 (внешнее изменение)