片段在屏幕旋转 [英] Fragment on Screen Rotation

查看:100
本文介绍了片段在屏幕旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个viewpager到其中包含两个页面的活动。

I have added a viewpager to an activity which contains two page.

在活动的onCreate我添加片段到fragmentAdapter:

In onCreate of activity I add fragments to a fragmentAdapter:

public void onCreate(Bundle savedInstanceState)
{
    ......

    FragmentAdapter fragmentAdapter = new FragmentAdapter
    (
        getSupportFragmentManager(),
        new Fragment[]{FragmentGame.builder(id), FragmentComments.builder(id)},
        new String[]{getString(R.string.gameInfo), getString(R.string.comments)}
    );

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(fragmentAdapter);

public static FragmentGame builder(long id)
{
    FragmentGame fragmentGame = new FragmentGame();

    // fragmentGame.id = id;

    Bundle bundle = new Bundle();
    bundle.putLong(Constants.EXTRA_ID, id);
    fragmentGame.setArguments(bundle);

    return fragmentGame;
}

第一次活动是建立在 onCreateView 片段被称为它的预期。

First time that activity is created the onCreateView of fragment is called as it's expected.

奇怪的现象是,当屏幕旋转时,第一次在 onCreateView 片段被称为两次,但第二个电话只有正确的 ID 并为第一叫 ID 0

The strange behaviour is when the screen is rotated for the first time the onCreateView of fragment is called twice but the second call only has the correct id and for the first call id is 0.

在第二个屏幕旋转, onCreateView 被称为三次,一次只有最后一个有编号。

On second screen rotation, onCreateView is called three times and again only the last one has id.

通过更大的屏幕旋转, onCreateView 要求增加。

By more screen rotation, onCreateView calls increase.

我发现有关片段以及屏幕旋转一些相关的问题,但我想不出为什么出现这种情况,如何做正确的方式。

I found some related question about fragment and screen rotation but I can't figure out why this happens and how to do it the right way.

-----更新------

----- UPDATE ------

该ID的问题就解决了​​,我代替束直接值的设置。

The ID problem is solved as I replaced bundle with direct value setting.

推荐答案

你旋转你正在创建一个新的片段,并将其添加到FragmentManager设备的每一个时间。

Every time you rotate your device you are creating a new fragment and adding it to the FragmentManager.

您所有的$ P $的pviously产生的碎片仍然在FragmentManager因此一个各一次。

All of your previously created fragments are still in the FragmentManager therefore the count increases by one each time.

如果您希望保留的值在你的片段,你需要将其存储在参数否则它包含在系统重新创建的片段将丢失任何值。

If you wish to retain a value in your fragment, you need to store it in the arguments otherwise any values it contains would be lost when the system re-creates the fragment.

public static FragmentGame builder(long id) {
 Bundle args = new Bundle();
 args.putInt("id", id);
 fragmentGame f = new fragmentGame();
 f.setArguments(args);
}

而不是创建一个新的片段,我怀疑你真的想找回previously创建了一个,当你旋转设备。使用 getSupportFragmentManager()。findFragmentById() getSupportFragmentManager()。findFragmentByTag()做到这一点。

编辑:额外的code

Extra code

    // Add fragment to the manager
    FragmentTransaction trans=getSupportFragmentManager().beginTransaction();
    trans.add(f,"myTag");
    trans.commit();

    // retrieve the fragment
    Fragment f= getSupportFragmentManager().findFragmentByTag("myTag");

只是试图检索片段,如果值为空,创建一个新的片段,并把它添加到管理器否则使用检索的一个

Just attempt to retrieve the fragment, if the value is null, create a new fragment and add it to the manager otherwise use the retrieved one.

这篇关于片段在屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆