Search code examples
androidandroid-3.0-honeycombandroid-fragments

Handling orientation changes with Fragments


I'm currently testing my app with a multipane Fragment-ised view using the HC compatibility package, and having a lot of difficultly handling orientation changes.

My Host activity has 2 panes in landscape (menuFrame and contentFrame), and only menuFrame in portrait, to which appropriate fragments are loaded. If I have something in both panes, but then change the orientation to portrait I get a NPE as it tries to load views in the fragment which would be in the (non-existent) contentFrame. Using the setRetainState() method in the content fragment didn't work. How can I sort this out to prevent the system loading a fragment that won't be shown?

Many thanks!


Solution

  • It seems that the onCreateViewMethod was causing issues; it must return null if the container is null:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {   
        if (container == null) // must put this in
            return null;
        return inflater.inflate(R.layout.<layout>, container, false);
    }