Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Orientation Change: Different Layout, Same Fragments

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) Different Layout, Same Fragments on Orientation Change

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?

like image 965
Ellesmera Avatar asked Jun 01 '15 02:06

Ellesmera


1 Answers

  • 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 enter image description here
  • 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 } }

like image 100
pRaNaY Avatar answered Oct 04 '22 01:10

pRaNaY