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

AndroidAndroid FragmentsAndroid ActivityRestart

Android Problem Overview


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"  />

Android Solutions


Solution 1 - Android

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"

Solution 2 - Android

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.

Solution 3 - Android

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"

Solution 4 - Android

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).

Solution 5 - Android

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.

Solution 6 - Android

I know this is quite late answer, but I recently faced this issue and was able to resolve this for Fragment Activity.

  1. In Manifest,

       android:configChanges="orientation|keyboardHidden|screenSize"
    
  2. In Class file, override the onSaveInstanceState(Bundle outState). Thats it! Enjoy :)

Solution 7 - Android

In Manifest file, inside activity add this line
android:configChanges="keyboard|keyboardHidden|orientation|screenSize" .

Solution 8 - Android

Add this to Manifeast.Xml

<android:configChanges="orientation|screenSize" >

Its work for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaza_BeView Question on Stackoverflow
Solution 1 - AndroidPJLView Answer on Stackoverflow
Solution 2 - AndroidJustLearningAgainView Answer on Stackoverflow
Solution 3 - AndroidThomas GattView Answer on Stackoverflow
Solution 4 - AndroidAarunView Answer on Stackoverflow
Solution 5 - AndroidEric FettmanView Answer on Stackoverflow
Solution 6 - AndroidPrachiView Answer on Stackoverflow
Solution 7 - AndroidR.SView Answer on Stackoverflow
Solution 8 - AndroidPrudhvi BsecureView Answer on Stackoverflow