2

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);
}
no9
  • 6,424
  • 25
  • 76
  • 115
  • I had this very same problem some time ago. I cannot remember what the solution was, sorry. If I remember I will answer. It might help others to answer if you post the relevant parts of the code. – Moog Jul 19 '11 at 10:44

6 Answers6

8

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.

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
Godsmith
  • 2,492
  • 30
  • 26
2

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

Graeme
  • 25,714
  • 24
  • 124
  • 186
  • yea i agree. But at the moment i dont need saving the values. It should work as the objects holding values for the fields are same as before screen orientation (the edittext controls read from this objects). As far as i can tell ... it should work. – no9 Jul 19 '11 at 11:42
1

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.

Sebastian Engel
  • 3,500
  • 32
  • 30
1

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.

manelizzard
  • 1,060
  • 8
  • 19
  • i dont think so. If i inspect the controls all seems fine. Ids, values .. everything. – no9 Jul 19 '11 at 10:57
  • and besides, the problem is not modifying one EditText, but all EditText controls get the same value (from the last one). – no9 Jul 19 '11 at 11:31
1

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

Community
  • 1
  • 1
Audrius
  • 2,836
  • 1
  • 26
  • 35
  • yes you are right. Currently im not saving anything. But it still should show the initial values and not same value for each EditText. – no9 Jul 19 '11 at 10:59
0

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

android:textIsSelectable="true"

from my XML layout.

Harry
  • 907
  • 10
  • 11