Wrong value in EditText when changing screen orien

2019-07-20 01:51发布

问题:

Im confused.

I have a layout (LinearLayout) holding a TextView and EditText. Somewhere in my code i create (programatically) a RelativeLayout and push in it several of this LinearLayouts. To each one of them i set an ID and values for TextView and EditText.

The problem is when i change screen orientation. All EditText controls gets the same value (from the last EditText). If i set a breakpoint all seems fine. I even inspected all elemets before setContentView is called and all values seem fine.

Any idea?

Here is the relevant part of code:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  csWgs84 = getCoordinateSystem();
  wgs84C = getCoordinates();
  sView = new ScrollView(this);
  layout = getLayoutInflater().inflate(R.layout.location_calculator, null);

  View control1 = getLayoutInflater().inflate(R.layout.text_edit, null);
  control1.setId(1);
  TextView title1 = (TextView) control1.findViewById(R.id.title);
  title1.setText(csWgs84.axisInfo.yAxisAbbreaviation + " [°] :");
  EditText value1 = (EditText) control1.findViewById(R.id.input);
  value1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value1.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.y, 1, 7, '.'), 12));
  RelativeLayout.LayoutParams viewLayoutParams1 = new RelativeLayout.LayoutParams
    (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
      viewLayoutParams1.addRule(RelativeLayout.BELOW, group.getId());
  ((RelativeLayout) layout).addView(control1, viewLayoutParams1);

  View control2 = getLayoutInflater().inflate(R.layout.text_edit, null);
  control2.setId(2);
  TextView title2 = (TextView) control2.findViewById(R.id.title);
  title2.setText(csWgs84.axisInfo.xAxisAbbreaviation + " [°] :");
  EditText value2 = (EditText) control2.findViewById(R.id.input);
  value2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value2.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.x, 1, 7, '.'), 12));
  RelativeLayout.LayoutParams viewLayoutParams2 = new RelativeLayout.LayoutParams
  (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
     viewLayoutParams2.addRule(RelativeLayout.BELOW, control1.getId());
    ((RelativeLayout) layout).addView(control2, viewLayoutParams2);

  sView.addView(layout, scrollViewLayoutParams);
  setContentView(sView);
}

回答1:

If the (admittedly usually very clever) default onPause() method isn't dealing well your activity being pushed to memory and being reloaded from it then you need to override it.

Use onPause to save the value's of your programatically created fields in a bundle. In your onCreate method check for the presence of a Bundle and create you're view's from this.

Activity Lifecycle



回答2:

I had the same problem. I don't know why it happens; it happens in one of my activities but not in another one that I think is almost identical. I haven't investigated it extensively though.

However, I came up with a workaround: Populate the EditTexts in onResume() instead of in onCreate(). (I also tried to populate them in onStart, but then I got the same fault.) I hope that may work for you or anyone else reading this thread.



回答3:

Be careful with the references. Maybe you are creating all the EditText "programatically", and then just modifying the last one several times.

Posting the code will be helpful.



回答4:

Check this or this. I think you are not saving EditText content during orientation change.



回答5:

Not sure if this is the case, but I was able to fix this issue by removing

android:textIsSelectable="true"

from my XML layout.



回答6:

The underlying problem is that the views in your custom layout, which is inflated and added multiple times, all have the same IDs (R.id.title and R.id.input). When Android saves their state, each one overwrites the previous one because of the same ID.

What you could do is to define some unique IDs which are generated by Android and assign those to the fields programmatically.