android:configChanges="方向"不适用于片段

发布于 2024-11-30 16:23:38 字数 457 浏览 0 评论 0 原文

我只是想调整我的一些应用程序以适应 HoneyComb。

我面临的问题是我的活动在方向变化(横向/纵向)时被破坏

当我使用经典活动时,我在清单中写道:

但现在,所有这些线路都不再起作用了!

有解决方法吗?

我的代码:

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

    <activity android:name=".TwitterActivity$AppListFragment"
    android:configChanges="keyboardHidden|orientation"  />

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尐籹人 2024-12-07 16:23:38

基于我使用 Honeycomb 3.0 和兼容性库 (r1) 的经验。

configChange="orientation" 确实可以与片段一起使用,以防止在方向更改时重新创建活动(它所应用的活动)。如果您希望在重新创建 Activity 时不重新创建 fragment,请在 onCreate 中调用 setRetainInstance

除非我遗漏了一些东西,否则我不太明白你的第二个清单条目,AppListFragment不是一个Fragment吗?如果是这样,那么为什么它会被列为清单中的一个条目?

请参阅 SO Answer 了解新的限定符,如果您的目标是 sdk 13,则可能会导致此问题,建议尝试 android:configChanges="orientation|screenSize"

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"

吝吻 2024-12-07 16:23:38

我遇到了一个非常相似的问题,但必须进行一些添加才能使其适用于各种版本(包括 ICS)。

在主应用程序活动中,我添加了与 Jason 提供的版本略有不同的版本。

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

我在 Honeycomb 之前就已经这样做了:

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

我必须制作第一个示例才能使其在所有版本上运行。我目前正在使用片段和 ActionBarSherlock 来实现向后兼容性。

我还更改了保存和重新加载的方式:

        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;
    }

保存实例状态方法的代码:

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

希望这会有所帮助。

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.

原来分手还会想你 2024-12-07 16:23:38

到 API 13 为止,configChanges 属性有了一个新值 screenSize

因此,如果您使用大屏幕,请确保在 configChanges 属性中添加 screenSize:

        android:configChanges="orientation|keyboardHidden|screenSize"

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"
花辞树 2024-12-07 16:23:38

即使没有碎片,我也遇到了同样的问题(即活动重新启动)。

我将: 更改

android:configChanges="orientation|keyboardHidden"

为:

android:configChanges="orientation|keyboardHidden|screenSize" 

防止活动重新启动。

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.

暖树树初阳… 2024-12-07 16:23:38

我知道这是一个很晚的答案,但我最近遇到了这个问题,并且能够解决片段活动的问题。

1) 在清单中,

      android:configChanges="orientation|keyboardHidden|screenSize"

2) 在类文件中,重写 onSaveInstanceState(Bundle outState)。
就是这样!享受 :)

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

羁拥 2024-12-07 16:23:38

在清单文件中,在活动内部添加此行

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

In Manifest file, inside activity add this line

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
望笑 2024-12-07 16:23:38

将此添加到 Manifeast.Xml

<android:configChanges="orientation|screenSize" >

它适合您。

Add this to Manifeast.Xml

<android:configChanges="orientation|screenSize" >

Its work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文