本文介绍了片段(支持库)不看onActivityCreated()活动,之后屏幕方向改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的支持库V13。有一个奇怪的事情我无法理解。

I'm using Android support library v13. There is a strange thing I couldn't understand.

在创建新的活动,我加载片段如下:

When creating new activity, I load fragment as:

主要业务布局:

...
<FrameLayout
    android:id="@+id/fragment_1"
    ... />

的onCreate()主要活动:

mFragment = (FragmentActivity) getSupportFragmentManager().findFragmentById(R.id.fragment_1);
// if screen orientation changed, no need to create new instance of fragment
if (mFragment == null) {
    mFragment = ...; // create new instance of fragment

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_1, mFragment);
    // because this is called ONCE, we can use this method
    ft.commitAllowingStateLoss();
}

现在,一切都完美的作品在模拟器1.5,1.6和2.2。我有一个手机的 2.2.2

Now, everything works perfectly in emulators 1.5, 1.6 and 2.2. I have a phone 2.2.2.

但有一个例外:如果应用程序正在运行,屏幕方向改变。在 onActivityCreated() getActivity()有时的回报。这只发生在仿真器1.5 / 1.6 / 2.2。

But there is an exception: if the app is running, and screen orientation changed. Inside onActivityCreated(), getActivity() sometimes returns null. This only happens in emulators 1.5/ 1.6/ 2.2.

我的手机的 2.2.2 的作品非常好,我测试几百次,但从来没有捕捉到的bug。即使是其他仿真3.x中,4.x的工作也很好。不幸的是我没有手机1.5 / 1.6 / 2.2。

My phone 2.2.2 works very well, I test hundreds of times but never catch that bug. Even other emulators 3.x, 4.x work well too. Unfortunately I don't have phone 1.5/ 1.6/ 2.2.

所以,你是否也有这方面的经验?是支持库的一个bug,或仿真?

So did you have experience with this? Is that a bug of the support library, or emulators?

推荐答案

更改Android设备的方向召回你的的onCreate 的方法,我相信。这样一来,奇怪的事情往往发生。有两件事情,你可能做的:

Changing Android device orientation recalls your onCreate method, I believe. As a result, weird things tend to happen. There are two things that you could potentially do:

1,您可以尝试追赶的方向变化,做一些code有尝试prevent从方向改变导致的问题:

1- You can try catching the orientation change and doing some code there to try to prevent the issues that result from orientation changing:

@Override
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.main);
        // etc.
}

2或prevent取向变更,加入你的清单干脆:的android:screenOrientation =肖像结果
你的主要活动标签,从reoccuring制止这个问题..也就是说,如果你愿意prevent方向变化。

2- Or prevent orientation-changing altogether in your manifest by adding: android:screenOrientation="portrait"
to your main activity tag, to stop this issue from reoccuring.. that is, if you are willing to prevent orientation changing.

我一般使用选项2我的应用程序,因为方位改变往往会造成各种各样的问题。

I generally use option 2 for my apps because orientation changing tends to cause all sorts of problems.

附注:我见过的人说,他们添加的android:configChanges =方向| keyboardHidden来的主要活动的清单,以解决一些定向的烦恼一样,所以这可能是值得一试为好。

Side note: I've seen people say they add android:configChanges="orientation|keyboardHidden" to their main activity's manifest to fix some orientation troubles as well, so that may be worth a try as well.

这篇关于片段(支持库)不看onActivityCreated()活动,之后屏幕方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:50