Ahmed Ezzat
Ahmed Ezzat

Reputation: 498

How to prevent opening a fragment from navigation drawer if that fragment is already opened

How to prevent opening a fragment from navigation drawer if that fragment is already opened, when the fragment is opened and click in the NavigationDrawer item the fragment reCreates again, So how to check if the fragment is already opened?

 public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    if (id == R.id.fragmentA) {
         {   FragmentA fragment = new FragmentA();
             transaction.add(R.id.main_screen, fragment1, "MainFrag").addToBackStack(null);
         }

    } else if (id == R.id.fragmentB) {
        FragmentB fragment = new FragmentB();
        transaction.add(R.id.main_screen, fragment).addToBackStack(null);

    } else if (id == R.id.fragmentC) {
        FragmentC fragment = new FragmentC();
        transaction.replace(R.id.main_screen, fragment).addToBackStack(null);

    }

Upvotes: 1

Views: 1550

Answers (4)

Patrick Lang
Patrick Lang

Reputation: 751

I have also struggled with this for a while, I am using navigation components and Kotlin, so finally I got it sorted out. It might not be the best solution, but it's simple and works, so I wanted to share it with you:

            when (menuItem.itemId) {

            R.id.nav_board -> {
                if(findNavController(R.id.nav_host_fragment).currentDestination?.label != "listFragment")
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.action_global_listFragment)
            }

The label is the destination label given in the navigation.xml

<navigation 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:id="@+id/navigation"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="at.example.app.ui.ListFragment"
        android:label="listFragment"
        tools:layout="@layout/fragment_list">
...

So this basically just checks if the current destination (where we're at) is the one that should not be re-opened and only navigates if the current destination is a different one.

Upvotes: 0

Sanjeet
Sanjeet

Reputation: 2403

While adding fragment use tags to replace it.

    transaction.replace(R.id.main_screen, fragment, tag);

Now, when menu items are clicked use fragment manager to get the visible fragment using tag :

    public boolean isFragmentCurrentlyVisible(String fragmentTag){
    Fragment fragmentToTest;
    fragmentToTest = fragmentManager.findFragmentByTag(fragmentTag);
    if(fragmentToTest!=null && fragmentToTest.isVisible())
        return true;
    return false;

}

Depending on value returned by above method you can add the fragment if not already added.

Upvotes: 0

Leo
Leo

Reputation: 1433

for example I am in the fragment B, And when I click in fragment B again from the NavigationDrawer it recreates, so how to check if the fragment is already shown in the screen or not?

If you have reference to Fragment B from the NavigationDrawer, you can call mFragmentB.isAdded() to know whether it's already shown or not.

Upvotes: 0

Eliran Tutia
Eliran Tutia

Reputation: 736

I solved it in this way:

public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    if (id == R.id.n_1 && !(f instanceof MainFragment)) {
        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    } }

Upvotes: 2

Related Questions