即使片段已更改,片段元素仍保留

时间:2016-03-26 14:21:18

标签: android android-layout android-fragments

我正在使用导航抽屉切换片段,但第一个片段中的元素即使在更改片段时仍然存在。

主要活动的代码:

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

        ...
        Drawer result = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .addDrawerItems(
                        item1,
                        new DividerDrawerItem(),
                        item2,
                        new SecondaryDrawerItem().withName(R.string.drawer_item_settings)
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        Fragment fragment = new Fragment();

                        switch (position)
                        {
                            case 0:
                                fragment = new HomeFragment();
                                Log.d("Switch", "Search");
                                break;
                            case 1:

                                break;
                            case 2:
                                fragment = new InventoryFragment();
                                Log.d("Switch", "Inventory");
                                break;
                            case 3:
                                fragment = new SettingsFragment();
                                Log.d("Switch", "Settings");
                                break;
                        }

                        transaction.replace(R.id.main_frame, fragment);
                        transaction.commit();
                        return false;
                    }
                })
                .build();
    }

以下是activity_main.xml

的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="?android:windowBackground"
    tools:context="com.example.nihal.xchange.MainActivity">

    ...

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/main_frame">
        <fragment
            android:name="com.example.nihal.xchange.HomeFragment"
            android:id="@+id/current_fragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

Home Fragment中的元素仍然存在于片段中,但其他片段的元素不会显示此行为。 我该如何防止这种情况发生?

1 个答案:

答案 0 :(得分:2)

不要在xml中包含Home Fragment。保持容器FrameLayout为空,并在活动开始后立即添加初始片段。在Activity的onCreate方法中,在开头动态添加Home片段。

activity_main.xml -

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

    <!-- Removed the fragment from here -->

</FrameLayout>

在onCreate()中,动态添加它 -

if (findViewById(R.id.main_frame) != null) {
        if (savedInstanceState != null)
            return;

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.main_frame, new HomeFragment());
    transaction.commit();
}