问题描述:

I have a similar problem like in this question: EditText not automatically saved on screen orientation change

High level view: I create UI from XML but recreate one or two TextViews programatically (only way to set a style from code). When I rotate the screen debugger shows that its text comes from activity XML not what user entered before rotation.

Code view:

activity_calc.xml:

 <TextView

android:id="@+id/operandFirst"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:singleLine="true"

android:text="50"

style="@style/FormulaValue" />

Activity.java:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_calc);

Log.d(getClass().getSimpleName(), "onCreate()" + ((TextView)findViewById(R.id.operandFirst)).getText());

if (formula == null && savedInstanceState == null) {

prepareNewFormula();

}

}

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

outState.putParcelable("formula", formula);

Log.d(getClass().getSimpleName(), "onSaveInstanceState()" + ((TextView)findViewById(R.id.operandFirst)).getText());

}

@Override

protected void onRestoreInstanceState(Bundle state) {

super.onRestoreInstanceState(state);

formula = state.getParcelable("formula");

Log.d(getClass().getSimpleName(), "onRestoreInstanceState()" + ((TextView)findViewById(R.id.operandFirst)).getText());

}

@Override

protected void onResume() {

super.onResume();

// HERE - getText() will return "50" which is default value in activity XML

Log.d(getClass().getSimpleName(), "onResume()" + ((TextView)findViewById(R.id.operandFirst)).getText());

unknown =((TextView)findViewById(R.id.operandFirst));

// COMMENTED OUT AFTER UPDATE: unknown = replaceView(view, R.layout.template_unknown_value, parent);

unknown.setText("");

}

// create TextView again at the same position with the same id

private TextView replaceView(TextView view, int template, LinearLayout parent) {

parent.removeView(view);

TextView textView = (TextView)getLayoutInflater().inflate(template, null);

textView.setId(view.getId());

int index = getWidgetPosition(view.getId());

parent.addView(textView, index, view.getLayoutParams());

return textView;

}

public void digitClicked(View view) {

CharSequence digit = ((TextView)view).getText();

unknown.append(digit);

}

The method replaceView will take id of XML with styled TextView:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=""

android:singleLine="true"

style="@style/FormulaUnknownValue" />

I wonder why the text is not preserved and why there is the very first original text that was replaced during startup.

--- UPDATE ---

I commented out TextView recreation. Though I still receive "50" from activity_calc.xml in onResume instead of value entered before rotation.

03-14 20:31:06.219 D/CalcActivity﹕ onCreate() 50

03-14 20:31:06.225 D/CalcActivity﹕ onResume() 50

03-14 20:31:54.138 D/CalcActivity﹕ onSaveInstanceState() 5

03-14 20:31:54.272 D/CalcActivity﹕ onCreate() 50

03-14 20:31:54.279 D/CalcActivity﹕ onRestoreInstanceState() 50

03-14 20:31:54.280 D/CalcActivity﹕ onResume() 50

相关阅读:
Top