Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am just trying to adapt some of my applications for HoneyComb.

The issue iI am facing is the destruction of my activity on orientation change (landscape/portrait)

When I was using a classic activity, I wrote in the manifest:

But now, all these lines aren't working anymore!

Is there a workaround for that?

My code:

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

    <activity android:name=".TwitterActivity$AppListFragment"
    android:configChanges="keyboardHidden|orientation"  />
like image 632
Waza_Be Avatar asked Aug 21 '11 16:08

Waza_Be


3 Answers

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"

like image 199
PJL Avatar answered Nov 14 '22 16:11

PJL


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.

like image 38
JustLearningAgain Avatar answered Nov 14 '22 16:11

JustLearningAgain


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"
like image 34
Thomas Gatt Avatar answered Nov 14 '22 17:11

Thomas Gatt