android:configChanges=“orientation” does not work with fragments

有些话、适合烂在心里 提交于 2020-01-18 04:52:36

问题


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

回答1:


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"




回答2:


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.




回答3:


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"



回答4:


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




回答5:


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.




回答6:


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




回答7:


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




回答8:


Add this to Manifeast.Xml

<android:configChanges="orientation|screenSize" >

Its work for you.



来源:https://stackoverflow.com/questions/7139488/androidconfigchanges-orientation-does-not-work-with-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!