android:configChanges="orientation" does not work with fragments

Based on my experience with Honeycomb 3.0 and compatibility library (r1).

configChange="orientation" does work with fragments with respect to preventing the activity (to which it is applied) being re-created on an orientation change. If you want the fragment not to be re-created on activity re-creation then call setRetainInstance in onCreate.

Unless I'm missing something I don't quite get your second manifest entry, isn't AppListFragment a Fragment? If so then why is it listed as an entry in your manifest?

See SO Answer for new qualifiers which is likely to be causing this if you are targetting sdk 13, suggest trying android:configChanges="orientation|screenSize"


I had a very similar problem but had to make a couple of additions to get it to work with various version (including ICS).

In the main app activity I added a slightly different version of what Jason offered.

<activity
android:name=".MyMainActivity"
android:configChanges="orientation|keyboardHidden|screenSize" 
android:label="@string/app_name" >

I had this working on pre-Honeycomb with:

           <activity
        ....
        android:configChanges="orientation|keyboardHidden" 
        .... >

I had to make the first example to get it running on all versions. I'm currently using fragments and ActionBarSherlock for backwards compatibility.

I also changed the way I was saving and reloading:

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Set up webview object
        View v = inflater.inflate(R.layout.webview_layout, container, false);
        webview = (WebView)v.findViewById(R.id.webview_fragment);
        webview.getSettings().setJavaScriptEnabled(true);

        // Check to see if it has been saved and restore it if true
        if(savedInstanceState != null)
        {
            if (savedInstanceState.isEmpty())
                Log.i(tag, "Can't restore state because bundle is empty.");
            else
            {
                if (webview.restoreState(savedInstanceState) == null)
                    Log.i(tag, "Restoring state FAILED!");      
                else
                    Log.i(tag, "Restoring state succeeded.");      
            }

        }
        else 
        {
            // Load web page
            webview.setWebViewClient(new MyWebViewClient());
            webview.getSettings().setPluginsEnabled(true);
            webview.getSettings().setBuiltInZoomControls(false); 
            webview.getSettings().setSupportZoom(false);
            webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
            webview.getSettings().setAllowFileAccess(true); 
            webview.getSettings().setDomStorageEnabled(true);
            webview.loadUrl(mTabURL);       
        }
        return v;
    }

The code for the save instance state method:

       @Override
    public void onSaveInstanceState(Bundle outState)
    {
        if(webview.saveState(outState) == null)
            Log.i(tag,"Saving state FAILED!");
        else
            Log.i(tag, "Saving state succeeded.");      
    }

Hope this helps.


Up to API 13 there was a new value to the configChanges attribute, screenSize

So if you're using large screens make sure to add screenSize in your configChanges attribute:

        android:configChanges="orientation|keyboardHidden|screenSize"

Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).


I was having this same problem (i.e., activity restarting) even without fragments.

I changed:

android:configChanges="orientation|keyboardHidden"

to:

android:configChanges="orientation|keyboardHidden|screenSize" 

That prevent the activity from restarting.