Nihal Harish
Nihal Harish

Reputation: 1060

Fragment elements remain even when the fragment has been changed

I'm using a navigation drawer to switch fragments, but elements from the first fragment stay present even on changing fragments.

The code of the main activity:

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

The following is the code for 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>

Elements from Home Fragment remain present across fragments but this behavior is not exhibited by elements of other fragment. How do I prevent this from happening?

Upvotes: 1

Views: 663

Answers (1)

Jyotman Singh
Jyotman Singh

Reputation: 11340

Don't include the Home Fragment in the xml. Keep your container FrameLayout empty and add the initial fragment as soon as the activity starts. In your Activity's onCreate method, add the Home fragment dynamically in the beginning.

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>

In the onCreate(), add it dynamically -

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

Upvotes: 2

Related Questions