Mark Delphi
Mark Delphi

Reputation: 1765

How to return back to fragment I was in right now in Navigation Component?

I have one Activity with four Fragments. Three of them are in DrawerLayout as Items. Around those three Fragments everything works fine. However, when I change to the last one, let's say TestFragment, that is not in DrawerLayout, by using navController.navigate(R.id.nav_test), and then come back to the HomeFragment by pressing on Back button, I can't enter in TestFragment any more.

Why do I can't reopen the left fragment again?

My codes:

MainActivity.kt:

class MainActivity : AppCompatActivity() {
    
    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var navController: NavController
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
    
        appBarConfiguration = AppBarConfiguration(setOf(R.id.nav_home, R.id.nav_up, R.id.nav_scan, R.id.nav_tst), drawer_layout)
        toolbar_layout.setupWithNavController(toolbar, navController, appBarConfiguration)
        setupActionBarWithNavController(navController, appBarConfiguration)
        nav_view.setupWithNavController(navController)
            
        fab.setOnClickListener {v ->
            navController.navigate(R.id.nav_tst)
        }
    
        override fun onSupportNavigateUp(): Boolean {
            return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
        }
    }
}

TstFragment:

class TstFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_tst, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }
}

Menu of DrawerLayout:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">
    
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_home"
            android:title="@string/home" />
        <item
            android:id="@+id/nav_up"
            android:icon="@drawable/ic_up"
            android:title="@string/up" />
        <item
            android:id="@+id/nav_scan"
            android:icon="@drawable/ic_scan"
            android:title="@string/scan" />
    </group>
</menu>

Navigation:

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.app.HomeFragment"
        android:label="@string/home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_nav_home_to_nav_tst"
            app:destination="@id/nav_tst" />
        <action
            android:id="@+id/action_nav_home_to_nav_scan"
            app:destination="@id/nav_scan" />
        <action
            android:id="@+id/action_nav_home_to_nav_up"
            app:destination="@id/nav_up" />
    </fragment>

    <fragment
        android:id="@+id/nav_up"
        android:name="com.example.app.UpFragment"
        android:label="@string/up"
        tools:layout="@layout/fragment_up" >
        <action
            android:id="@+id/action_nav_up_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>

    <fragment
        android:id="@+id/nav_scan"
        android:name="com.example.app.ScanFragment"
        android:label="@string/scan"
        tools:layout="@layout/fragment_scan" >
        <action
            android:id="@+id/action_nav_scan_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>

    <fragment
        android:id="@+id/nav_tst"
        android:name="com.example.app.TstFragment"
        android:label="@string/tst"
        tools:layout="@layout/fragment_tst" >
        <action
            android:id="@+id/action_nav_tst_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>
</navigation>

Upvotes: 0

Views: 1222

Answers (2)

Mark Delphi
Mark Delphi

Reputation: 1765

I could resolve my problem. For those who look for a solution. I need to remove the fab from the MainActivity and call it in my HomeFragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    requireActivity().findViewById<FloatingActionButton>(R.id.fab).setOnClickListener {
        Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
                    .navigate(R.id.nav_tst)
        }
    }
}

And also I had to add the following line in onViewCreated for leaving the TstFragment:

requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
    override fun handleOnBackPressed() {
        Navigation.findNavController(view).popBackStack(R.id.nav_tst, true))
    }
}

Exactly in that way I could solve it.

Upvotes: 1

Eze larry
Eze larry

Reputation: 36

            getSupportFragmentManager().beginTransaction().replace(R.id.frag_frame, fragment).addToBackStack("just add any text").commit();

Upvotes: 0

Related Questions