片段恢复对定向状态改变片段、状态

2023-09-08 09:53:02 作者:秒爆⊕高手℡

我要实现我的应用程序非标准的片段导航(请参阅链接)。

I have to implement "standart" fragments navigation in my app (please see link).

的问题是当设备处于纵向模式,有应显示仅1片段,并且当其转动到横向模式,2片段应该显示。

The issue is when device is in portrait mode, there should be shown only 1 fragment, and when it is rotated to landscape mode, 2 fragments should be shown.

我试着做这两种不同的方式:

I tried to do this 2 different ways:

1)我用不同横向和纵向布局,只有1活动。

纵向布局的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/main_frame_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

和here`s景观布置:

And here`s landscape layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/main_frame_fragment_container_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/main_frame_fragment_container_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

Activity`s的的onCreate 的方法:

    private static ItemsFragment mItemsFragment;
    private static ItemDetailsFragment mItemDetailsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (mItemsFragment == null) {
            mItemsFragment = ItemsFragment.newInstance();
        }
        if (mItemDetailsFragment == null) {
            mItemDetailsFragment = ItemDetailsFragment.newInstance();
        }

        if (isLandscape()) {
            getSupportFragmentManager().beginTransaction().replace(R.id.main_frame_fragment_container_left, mItemsFragment)
                    .commit();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.main_frame_fragment_container_right, mItemDetailsFragment).commit();
        } else {
            getSupportFragmentManager().beginTransaction().replace(R.id.main_frame_fragment_container, mItemsFragment)
                    .commit();
        }
    }

和that`s的方式我刷新第二片段:

And that`s the way I refresh 2nd fragment:

Bundle bundle = new Bundle();
bundle.putSerializable(BaseFragment.KEY_BUNDLE_ITEM, response.getItem());
mItemDetailsFragment = ItemDetailsFragment.newInstance(bundle);
if (isLandscape()) {
    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.main_frame_fragment_container_right, mItemDetailsFragment).commit();
} else {
    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.main_frame_fragment_container, mItemDetailsFragment).addToBackStack(null).commit();
}

此外,我保存和恢复fragments`状态,所以旋转后,我的数据不会消失。一般来说,这个code正常工作,在我的情况。

Also I save and restore fragments` states, so my data does not disappear after rotations. Generally, this code works properly in my case.

2)我用2活动,并为活动一日纵向和横向模式相同的布局。

XML布局是一样的previous一个景观:    

xml layout is the same as in previous one for landscape:

    <FrameLayout
        android:id="@+id/main_frame_fragment_container_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/main_frame_fragment_container_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

的的onCreate 的方法(注意,该片段实体不是静态的,因为它是在第一种情况):私人ItemsFragment mItemsFragment;    私人ItemDetailsFragment mItemDetailsFragment;

onCreate method (note, that fragments entities are not static, as it was in 1st case): private ItemsFragment mItemsFragment; private ItemDetailsFragment mItemDetailsFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        mItemsFragment = ItemsFragment.newInstance();
        mItemDetailsFragment = ItemDetailsFragment.newInstance();

        getSupportFragmentManager().beginTransaction().replace(R.id.main_frame_fragment_container_left, mItemsFragment)
        .commit();
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_frame_fragment_container_right, mItemDetailsFragment).commit();
    }
}

现在,如果该设备处于纵向模式时,我开始新的活动:

And now if the device is in portrait mode, I start new Activity:

if (isLandscape()) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(BaseFragment.KEY_BUNDLE_ITEM, response.getItem());
    mItemDetailsFragment = ItemDetailsFragment.newInstance(bundle);
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_frame_fragment_container_right, mItemDetailsFragment).commit();
} else {
    Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);
    intent.putExtra(KEY_ITEM, response.getItem());
    startActivity(intent);
}

和,最后,第2 Activity`s的的onCreate 的方法:

And, at last, 2nd Activity`s onCreate method:

protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.activity_details);

    if (isLandscape()) {
        finish();
    }

    Item item = (Item) getIntent().getExtras().getSerializable(KEY_ITEM);

    Bundle bundle = new Bundle();
    bundle.putSerializable(BaseFragment.KEY_BUNDLE_ITEM, item);

    ItemDetailsFragment mItemDetailsFragment = ItemDetailsFragment.newInstance(bundle);

    getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_frame_fragment_container, mItemDetailsFragment).commit();
}

当设备旋转为横向模式,第二活动结束后,我看到我的2片段(如预期)1日的活动。

When device is rotated to landscape mode, 2nd activity finishes, and I see my 1st activity with 2 fragments (as expected).

问:

在第一种情况我片段保存为静态变量,也正因为如此,如果我改变纵向或横向模式的第二个片段的状态我不在乎(相同的片段时)。但我不认为这是一个好主意,将其保存为静态字段。

In 1st case I save fragments as static variables, and because of this I don't care if I change 2nd fragment state in portrait or landscape modes (the same fragment is used). But I don't think it's a good idea to save it as static fields.

在第二个情况下,我不知道如何同步活性的片段B(横向)与活动B片段B(人像)。如果我改变片段的东西(我的意思是,切换按钮等)和旋转装置,改变应该在另一个片段被应用。

In 2nd case I don't know how to sync Activity A Fragment B (landscape) and Activity B Fragment B (portrait). If I change something in fragment (I mean, toggle button etc) and rotate device, changes should be applied in another fragment.

一般情况下,什么情况下是较好的,如果2,我怎么能解决同步问题?或者,也许还有另一种更简单的方法。感谢您的阅读,我希望你能帮助我:)

Generally, what case is better, and if 2nd, how can I resolve synchronization issue? Or maybe there is another easier way. Thanks for reading, I hope you can help me :)

推荐答案

只要按照这个队友 http://developer.android.com/guide/components/fragments.html 。不要使碎片静态的(那只是奇怪)

Just follow this mate http://developer.android.com/guide/components/fragments.html. Dont make fragments static (thats just wierd)