cancellation of the content of the EditText when changing from PORTRAIT to Landscape and landscape to portrait

StackOverflow https://stackoverflow.com/questions/20273082

문제

In my application, the layout is created in java following this code:

frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
frameLayout.setBackgroundResource(R.drawable.lavagna1);       
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

inflater = (LayoutInflater) SecondaAttivitaEQ.this.getSystemService(LAYOUT_INFLATER_SERVICE);

layout = inflater.inflate(R.layout.keyboardmatrix,(ViewGroup) findViewById(R.id.ciao));

    for ( int i = 0; i < grado; i++) {
        tableRow = new TableRow(this); 
        tableRow.setGravity(Gravity.CENTER);

        for (int j = 0; j < 1; j++) {

            valore[i][j].setHint(" c" + i + " ");
            valore[i][j].setPadding(10, 10, 10, 10);
            tableRow.addView(valore[i][j]);
            tableLayout.addView(tableRow);

        }
     }

    VSC.addView(tableLayout);
    HSC.addView(VSC);
    frameLayout.addView(HSC);
    setContentView(frameLayout);

    secondo = new Button (this);
    secondo.setText("SOLVE");
    secondo.setTextColor(0xffffffff);
    secondo.setBackgroundResource(R.drawable.but_ok);
    tableLayout.addView(secondo);

This code creates a table of EditText, the problem lies in the rotation of the screen, in fact if I enter values ​​in the EditText and the phone is rotated, the app recreates the layout by deleting all of the content of the EditText. Can you help me solve this problem?

도움이 되었습니까?

해결책

Everytime orientation change, android create new view and destroy the old one. You can save your data when orientation change and re-initialize when the new view is created

Use onConfigurationChanged method of activity to detect Orientation Change

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
  }

Update AndroidManifest.XML like this to include the android:configChanges

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top