Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent the layout from getting reset when the screen orientation changes?
    text
    copied!<p>My example is adapted from one of the sample applications that come with one of the compatibility libraries. That's the layout file:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dp"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/fragment_menu_msg" /&gt; &lt;CheckBox android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:checked="true" android:text="@string/fragment1menu"&gt; &lt;/CheckBox&gt; &lt;CheckBox android:id="@+id/menu2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:checked="true" android:text="@string/fragment2menu"&gt; &lt;/CheckBox&gt; &lt;/LinearLayout&gt; </code></pre> <p>And here some Java code:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_menu); ViewGroup layout = (ViewGroup) findViewById(R.id.layout); Button btn = new Button(getApplication()); btn.setId(1); btn.setText("Button 1"); layout.addView(btn); mCheckBox1 = (CheckBox) findViewById(R.id.menu1); mCheckBox1.setOnClickListener(mClickListener); } final OnClickListener mClickListener = new OnClickListener() { public void onClick(View v) { ViewGroup layout = (ViewGroup) findViewById(R.id.layout); Button btn = new Button(v.getContext()); btn.setId(2); btn.setText("Button 2"); layout.addView(btn); } }; </code></pre> <p>The activity starts, you disable the first checkbox, one button (button 2) is added to the layout, you change the screen orientation and button 2 (added in the onClickListener method) disappears.</p> <p><a href="http://s14.directupload.net/file/d/2931/zvqkubis_png.htm" rel="nofollow">Screenshots on Directupload.net</a></p> <p>I have read a lot of stuff about <a href="http://developer.android.com/guide/topics/resources/runtime-changes.html" rel="nofollow">Handling Runtime Changes</a> but I still don't have a clear picture how to prevent Button 2 to disappear in the avobe example. Any help is highly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload