旋转后TextView内容丢失

时间:2015-03-14 18:21:17

标签: android android-activity

我遇到类似问题的问题:EditText not automatically saved on screen orientation change

高级视图:我从XML创建UI,但以编程方式重新创建一个或两个TextView(仅从代码设置样式的方式)。当我旋转屏幕时,调试器显示其文本来自活动XML而不是用户在轮换之前输入的内容。

代码视图:

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

方法replaceView将带有带有样式TextView的XML的id:

<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" />

我想知道为什么没有保留文本以及为什么在启动期间有第一个原始文本被替换。

---更新---

我评论了TextView娱乐。虽然我仍然从activity_calc.xml中的onResume收到“50”,而不是在轮播前输入的值。

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

0 个答案:

没有答案