3

I have layout/activity_main.xml and layout-land/activity_main.xml. When I change orientation the activity reload again.

I try to use this code android:configChanges="keyboardHidden|orientation" and it work the activity is not reloading just what i like but the problem is the layout is not switching from layout/activity_main.xml to layout-land/activity_main.xml.

How do i switch from layout/activity_main.xml to layout-land/activity_main.xml. without refreshing the activity.

BSavaliya
  • 809
  • 1
  • 16
  • 26
Zero_73
  • 183
  • 3
  • 16
  • possible duplicate of [Android orientation change calls onCreate](http://stackoverflow.com/questions/3097909/android-orientation-change-calls-oncreate) – Jared Burrows Mar 02 '15 at 03:05
  • Ya i see that but my question has a different. Just what i said `android:configChanges="keyboardHidden|orientation"` is working but the layout is not change from `layout/activity_main.xml` to `layout-land/activity_main.xml` – Zero_73 Mar 02 '15 at 03:33

2 Answers2

6

By declaring android:configChanges="orientation|screenSize" you instruct Activity Manager to not restart your activity and let you handle configuration change via onConfigurationChanged().

If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

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

Which means that onCreate() will be skipped on configuration changed, and you cannot swap your layout (since onCreate() is where you recreate the view).

In your case, you want to change layout, so there is no choice but to refresh your activity, which means to remove android:configChanges="orientation|screenSize". If you want to keep the state, you can save and check the Bundle passed to your onCreate() to restore state accordingly.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // set your layoutId according to landscape/portrait
    setContentView(layoutId);
    if (savedInstanceState != null) {
        // restore your state here
    }
    ...
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // save your state here
}

For more reference: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

hidro
  • 12,333
  • 6
  • 53
  • 53
  • Thank for explanation i understand now my case. I can't use this code ` android:configChanges="orientation|screenSize"` because its prevent to create new layout for my new orientation but How do i save my state? sorry i dont get that part "saving the state" I'm new in android dev. – Zero_73 Mar 02 '15 at 03:58
  • Check out this training doc: http://developer.android.com/training/basics/activity-lifecycle/recreating.html – hidro Mar 02 '15 at 04:05
  • Genius! Thank you – Sarah Khan Apr 07 '21 at 01:16
4

Try using this:

android:configChanges="orientation|screenSize"

in your Android Manifest in your <activity> tag to restrict the reloading of the activity.

YoussefDir
  • 287
  • 1
  • 3
  • 16
Tristan
  • 3,530
  • 3
  • 30
  • 39
  • As you can see i mention that in my question and its work but my problem is if i use that code the layout is not switching from `layout/activity_main.xml` to `layout-land/activity_main.xml`. – Zero_73 Mar 02 '15 at 03:35