How to prevent layout creation when reset when changing screen orientation?

My example is adapted from one of the sample applications that come with one of the compatibility libraries. This is the layout file:

<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"> <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" /> <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"> </CheckBox> <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"> </CheckBox> </LinearLayout> 

And here is the Java 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); } }; 

The action begins, you turn off the first flag, one button (button 2) is added to the layout, you change the screen orientation, and button 2 (added in the onClickListener method) disappears.

Screenshots at Directupload.net

I read a lot about "Processing Runtime Changes" , but I still do not have a clear idea of ​​how to avoid pressing button 2 in the example on avob. Any help is appreciated.

+4
source share
4 answers

If you want to set the Screen in portrait mode and want to stop the activity for recreation, while the orientation changes, just enter the code, as shown below, into your activity.

the code:

 <activity android:name=".YourActivityName" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation"></activity> 

This code helps me and will also work for you.

Enjoy.

:)

Happy coding ...

0
source

The activity is restarted when you change the orientation, and all its state is lost (in your case, a button is dynamically added). You must be prepared for this, because there are other situations where this can happen. For example, your application may be killed if it is in the background and the device needs to free up memory.

The correct solution is to maintain the state of activity until it is destroyed, and then restore it after it is recreated. Something like that:

 @Override protected void onSaveInstanceState(Bundle outState) { // save state of your activity to outState } @Override public void onCreate(Bundle savedInstanceState) { if (savedInstanceState != null) { // restore state of your activity from savedInstanceState } } 

For more information, see "Activity Retention Status" in the official documentation.

0
source

put this entry in the manifest with activity and override the onConfigurationChanged function

 android:configChanges="orientation|keyboardHidden|screenSize" @Override public void onConfigurationChanged(Configuration newConfig) { // super.onConfigurationChanged(newConfig); } 

this will stop the re-creation of activity when the orientation changes ......

-1
source

Do your job to look something like this in a manifest

  <activity android:name=".Hello" android:label="@string/app_name" android:screenOrientation="portrait" <-- Screen will be forced to have portrait android:configChanges="orientation|keyboardHidden|screensize" > <-- No Restart in these cases 

The ScreenSize attribute seems to be added in 4.0, so don't mention it if you scold below 4.0.

-1
source

All Articles