Android Orientation Change: Different Layout, Same Fragments

∥☆過路亽.° 提交于 2020-01-23 06:11:11

问题


This is pretty much a classic Android use case.

Say we have 2 fragments: FragmentA and FragmentB.

In landscape mode, FragmentA and FragmentB sit side by side.

In portrait mode, they each take up the full screen when in use.

(See this image but replace tablet->landscape and handset->portrait)

As explained here (Supporting Single-Pane and Multi-Pane Layouts), there are 2 ways to achieve this:

1- Multiple fragments, one activity: Use one activity regardless of the device size, but decide at runtime whether to combine fragments in the layout (to create a multiple-pane design) or swap fragments (to create a single-pane design).

2- Multiple fragments, multiple activities: On a tablet, place multiple fragments in one activity; on a handset, use separate activities to host each fragment. For example, when the tablet design uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just the first fragment. When running on a handset and you need to switch fragments (such as when the user selects an item), start another activity that hosts the second fragment.

This makes a lot of sense in theory, but I'm hitting some hurdles in trying to actually implement either of these approaches in a way that restores the state of the Fragments when the orientation changes and on back button press.

I tried the first approach and got quite far, but found it messy because it required managing all the fragment transactions manually, in particular because the container of a Fragment cannot be changed easily. It is also a pain to decide what to do on back pressed because the last transaction on the backstack might belong to the other orientation.

Now I am trying the second option. It seems cleaner so far but the Fragments get recreated from scratch on every orientation change (because each orientation uses a different Activity). I would like to have a way to restore the Fragment state from the other Activity/orientation.

Can anyone explain how this can be done, or point me to an appropriate tutorial or sample application?


回答1:


  • add this configuration in MainActivity where Fragment are replace <activity android:name=".MainActivity" android:configChanges="orientation|screenSize|keyboard|keyboardHidden" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT"/></intent-filter> </activity
  • You have create fragment landscape layout for FragmentA and FragmentB

  • if you need to change behavior of both fragment you should change in onConfigurationChanged event of fragment.

@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //write your stuff for landscape orientation }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //write your stuff for portrait orientation } }



来源:https://stackoverflow.com/questions/30564737/android-orientation-change-different-layout-same-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!