Search code examples
androidkotlinnavigation

how to show back press button in a fragment


yes there are a lot of results about it i tried but i couldn't find the right answer. I have a navigation graph team. I have an activity and Four Fragments. I want to put the back press button in the transition between the fragments


Solution

  • Just use findNavController, it is easy to navigate but be sure to add action to your navigation. It will add a back button automatically. For connect to each other: nav_graph:

    <?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/my_nav"
        app:startDestination="@id/firstFragment">
    
        <fragment
            android:id="@+id/firstFragment"
            android:name="com.example.testbackbutton.FirstFragment"
            android:label="fragment_first"
            tools:layout="@layout/fragment_first" >
            <action
                android:id="@+id/action_firstFragment_to_secondFragment"
                app:destination="@id/secondFragment" />
        </fragment>
        <fragment
            android:id="@+id/secondFragment"
            android:name="com.example.testbackbutton.SecondFragment"
            android:label="fragment_second"
            tools:layout="@layout/fragment_second" >
            <action
                android:id="@+id/action_secondFragment_to_firstFragment"
                app:destination="@id/firstFragment" />
        </fragment>
    </navigation>
    

    MainActivity:

    class MainActivity : AppCompatActivity() {
        private lateinit var binding: ActivityMainBinding
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivityMainBinding.inflate(layoutInflater)
            setContentView(binding.root)
    
            setupActionBarWithNavController(findNavController(R.id.fragmentContainerView))
    
        }
        override fun onSupportNavigateUp(): Boolean {
            val navController = findNavController(R.id.fragmentContainerView)
            return navController.navigateUp() or super.onSupportNavigateUp()
        }
    }
    

    First fragment:

    class FirstFragment : Fragment() {
        private var _binding: FragmentFirstBinding? = null
        private val binding get() = _binding!!
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
    
    
            _binding = FragmentFirstBinding.inflate(inflater, container, false)
    
            binding.btn.setOnClickListener{
                findNavController().navigate(R.id.action_firstFragment_to_secondFragment)
            }
    
            return binding.root
        }
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            setHasOptionsMenu(true)
        }
    } 
    

    First fragment XML:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".FirstFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_blank_fragment"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    The second fragment is the default. Click the button on the first fragment then you will see the back button on the second fragment.