BRDroid
BRDroid

Reputation: 4388

Navigating back to previous fragment - NavGraph

I am navigating from one fragment to another using NavGraph - Navigation UI component.

The physical button the device works for navigation, but how do i activate the back arrow on the top of app to go back to previous fragment

enter image description here

NavGraph

 <?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"
    android:id="@+id/nav_graph"
    app:startDestination="@id/charactersFragment">

    <fragment
        android:id="@+id/charactersFragment"
        android:name="com.example.breakingbad.CharactersFragment"
        android:label="fragment_characters"
        tools:layout="@layout/fragment_characters" >
        <action
            android:id="@+id/action_charactersFragment_to_characterDetailFragment"
            app:destination="@id/characterDetailFragment" />
    </fragment>
    <fragment
        android:id="@+id/characterDetailFragment"
        android:name="com.example.breakingbad.CharacterDetailFragment"
        android:label="fragment_character_detail"
        tools:layout="@layout/fragment_character_detail" />
</navigation>

CharacterDetailFragment - this does not do much at the moment

class CharacterDetailFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }

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

Activity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }
}

EDIT

Updated the activity code to this and it worked

class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navController = Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}

Thanks R

Upvotes: 1

Views: 2035

Answers (3)

Anwar Zahid
Anwar Zahid

Reputation: 257

If you implement this with the navigation drawer layout then use this line for back to the previous fragment But if you want to implement the navigation component with Bottom navigation and navigation drawer layout click here. Just it explains for future help.

appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayoutID)
NavigationUI.setupActionBarWithNavController(this, navController,
     drawerLayoutID)

Upvotes: 0

Abdulrahman AlGhamdi
Abdulrahman AlGhamdi

Reputation: 146

Simply Add onSupportNavigateUp() to your main activity

override fun onSupportNavigateUp() = navController.navigateUp()

Upvotes: 2

Gavin Wright
Gavin Wright

Reputation: 3212

We call that the "Up" navigation button. See here for discussion about this issue:

How to handle up button inside fragment using Navigation Components

The solution will, at the very least, require using the setupActionBarWithNavController() method.

Upvotes: 4

Related Questions