5

My example is adapted from one of the sample applications that come with one of the compatibility libraries. That's 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 some 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 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.

Screenshots on Directupload.net

I have read a lot of stuff about Handling Runtime Changes 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.

abatishchev
  • 95,331
  • 80
  • 293
  • 426
StSch
  • 263
  • 1
  • 3
  • 12
  • This is already discussed http://stackoverflow.com/questions/2730855/prevent-screen-rotation-android – Nuno Gonçalves Jun 24 '12 at 08:34
  • 1
    Please note this (android:screenOrientation="portrait") is just hiding a bug in your app, making it less likely for users to trip over it. – StSch Jun 24 '12 at 09:52

4 Answers4

1

Activity restarts when you change orientation and all its state is lost (in your case dynamically added button). You must be prepared to handle this as there are other situations where this might happen. For example your application might be killed if it is in background and a device needs to free memory.

The correct solution is to save the state of the activity before it is destroyed and then restore it once it is recreated. Something like this:

@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
    }
}

See Saving activity state topic in official documentation for more details.

romario333
  • 587
  • 2
  • 11
0

If you want to set the Screen in Portrait mode and want to stop the activity to recreating while the orientation change then just put like below code to your activity.

Code:

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

This code helps me and will also works for you.

Enjoy.

:)

Happy Coding...

Shreyash Mahajan
  • 22,842
  • 35
  • 109
  • 188
-1

put this entry in manifest with activity and Override onConfigurationChanged function

android:configChanges="orientation|keyboardHidden|screenSize"

@Override
public void onConfigurationChanged(Configuration newConfig) {
   // super.onConfigurationChanged(newConfig);


}

this will stop re-creation of the activity on the orientation change......

Dheeresh Singh
  • 15,570
  • 3
  • 36
  • 36
-1

Make your activity to look something like this in 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

ScreenSize attribute seems to be added in 4.0 so don't mentioned it if you are ruuning below 4.0.

Vipul
  • 31,716
  • 7
  • 69
  • 86
  • The Android Developer's Guide at http://developer.android.com/guide/topics/manifest/activity-element.html#config says: Using this attribute (android:configChanges) should be avoided and used only as a last-resort. Still no idea what the proper way is supposed to be. – StSch Jun 24 '12 at 09:48