简体   繁体   中英

why dynamically created elements will be cleaned in java(android)?

I am developing an aplication by java for android.in my app i create some TableRows dynamically by code.when i rotate or go to another activity and return,my created elements will be cleaned.why? I add

android:configChanges="keyboardHidden|orientation"

to my my Manifest file by my problem not resolved.

my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
 package="com.example.alis"
 android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name=".Login"
    android:label="@string/title_activity_login" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".Mainmenu"
    android:label="@string/title_activity_mainmenu" >
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".CustomerList"
    android:label="@string/title_activity_customer_list" >
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SelectProductActivity"
    android:configChanges="keyboardHidden|orientation|screensize"
    android:label="@string/title_activity_select_product" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
   </application>

my activity:

public class SelectProductActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_product);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_select_product, menu);
    return true;
}

public void addRow(View v){
   TableLayout tbl=(TableLayout)findViewById(R.id.tblProduct);
   TableRow row=new TableRow(this);
   TableRow.LayoutParams llp = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
   llp.setMargins(0,10,0,10);
   llp.bottomMargin=20;
   row.setLayoutParams(llp);
   row.setBackgroundColor(Color.parseColor("#eeeeee"));

   //
   TextView tv=new TextView(this);
   tv.setText("txt1");
   tv.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );

   row.addView(tv);
   //
   EditText ed=new EditText(this);
   ed.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(ed);
   //
   TextView tv2=new TextView(this);
   tv2.setText("txt2");
   tv2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(tv2);

   //
   EditText ed2=new EditText(this);
   ed2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(ed2);

   //
   TextView tv3=new TextView(this);
   tv3.setText("txt3");
   tv3.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, .40f ) );

   row.addView(tv3);
   tbl.addView(row);
     }

  }

As of Android 3.2, you also need to add "screenSize":

android:configChanges="keyboardHidden|orientation|screenSize"

Mind the upper S in screenSize!

Source: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM