0

I have an Activity (which is the launcher of the application) which has a ViewPager as a navigation menu between tabs. Inside this ViewPager I have different type of fragments.

Currently, I'm not handling the orientation changes myself, so the Activity is re-created all over again while I'm rotating my device. (Which behave quite good but I'm not looking into loading the whole activity again because of long loading time and bad user experience).

For a specific Fragment I have 2 layouts (One for portrait and one for landscape).

What I need is to keep the state of the activity and the fragment but to replace fragment layout on orientation change. My problem is that it has extra views in the landscape layout than in portrait so I don't know how to handle it.

I've tried configChanges, but obvisouly it doesn't change my layout. I tried inflate the portrait and landscape layouts and replace them inside a FrameLayout container on onConfigurationChanged, but it doesn't seem to work either.

How can I handle a situation where I need to load a different layout while keeping the fragment state untouched ? Is this even possible ? What is the right approach for this kind of thing ?

Gilad Eshkoli
  • 1,253
  • 11
  • 27

1 Answers1

1

I have such a situation, to load different layout on changing screen orientation through setLayout method;

public class SampleFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.fragment_layout, null);
    return mRoot;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    this.init();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    this.setLayout();
}

private void init() {
    this.setLayout();
}

private void setLayout() {
    View view = null;
    int orientation = mResources.getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        view = mInflater.inflate(R.layout.segment_dettaglio_evento_land, null);
    else if (orientation == Configuration.ORIENTATION_PORTRAIT)
        view = mInflater.inflate(R.layout.segment_dettaglio_evento, null);

    if (orientation == Configuration.ORIENTATION_LANDSCAPE || orientation == Configuration.ORIENTATION_PORTRAIT) {
        ViewGroup viewGroup = (ViewGroup) mRoot.findViewById(R.id.dettaglio_evento_root);
        viewGroup.removeAllViews();
        viewGroup.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        this.initComponent();
    }
}

private void initComponent() {
 if (mResult == null)
        retrieveData();
    else
        populateWithDetail();
}

}

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dettaglio_evento_root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/dettaglio_evento_background"
  android:orientation="vertical" >
</LinearLayout>

I also have a variable mResult that contains the data kept from network request, so if it's null I make the request, populate views otherwise.

roccocullo
  • 178
  • 11