Home > Back-end >  Redirect a Fragment to another Fragment
Redirect a Fragment to another Fragment

Time:04-30

I'm studying a lot about Kotlin, and I have a single project to make. So, I have some Fragments and one Activity. All actions from left menu are working, ok. But in CalendarioFragment, I need to redirect to another Fragment when the user press a button. Now, I don't know how to do this. I am using Nav Graph, with navhost enabled in both.

    <?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">

    <fragment
        android:id="@ id/calendarioFragment"
        android:name="com.example.aprimorandoconhecimentoss.ui.calendario.CalendarioFragment"
        android:label="fragment_calendario"
        tools:layout="@layout/fragment_calendario"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation">
        <action
            android:id="@ id/action_calendarioFragment_to_pdfFragment"
            app:destination="@id/pdfFragment"
            app:launchSingleTop="false" />
    </fragment>
    <fragment
        android:id="@ id/pdfFragment"
        android:name="com.example.aprimorandoconhecimentoss.ui.pdf.PdfFragment"
        android:label="fragment_pdf"
        tools:layout="@layout/fragment_pdf"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation"/>
</navigation>

My MainActivity is as:

class MainActivity : AppCompatActivity() {
    
        private lateinit var appBarConfiguration: AppBarConfiguration
        private lateinit var binding: ActivityMainBinding
        private lateinit var pdftela: FragmentPdfBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            val navHostFragment =
                supportFragmentManager.findFragmentById(R.id.calendarioFragment) as NavHostFragment
            val navCont = navHostFragment.navController
    
            binding = ActivityMainBinding.inflate(layoutInflater)
            setContentView(binding.root)
    
            setSupportActionBar(binding.appBarMain.toolbar)
    
            val drawerLayout: DrawerLayout = binding.drawerLayout
            val navView: NavigationView = binding.navView
            val navController = findNavController(R.id.nav_host_fragment_content_main)
    
    
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top level destinations.
            appBarConfiguration = AppBarConfiguration(
                setOf(
                    R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_calendario_academico
                ), drawerLayout
            )
            setupActionBarWithNavController(navController, appBarConfiguration)
            navView.setupWithNavController(navController)
    
    
        }
    
        override fun onSupportNavigateUp(): Boolean {
            val navController = findNavController(R.id.nav_host_fragment_content_main)
            return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
        }
    }

I tried to put supportFragmentManager on MainActivity, but this way, app crashes on init. Is there a way to redirect from Fragment locally to another Fragment? 



class CalendarioFragment : Fragment() {
    
        private var _binding: FragmentCalendarioBinding? = null
    
        // This property is only valid between onCreateView and
        // onDestroyView.
        private val binding get() = _binding!!
        var navc = NavController
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            val v: View = inflater.inflate(R.layout.fragment_calendario, container, false)
    
    
    
            val listener = view?.setOnClickListener { view ->
                when (view?.id)  {
                    R.id.botao_view1->{
    
                    }
                }
    
    
            }
    
            return v
        }

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

Thanks in advance.

CodePudding user response:

MainActivity

button.setOnClicListener{
   setCurrentFragment(CalendarioFragment )
}



private fun setCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.calendarioFragment, fragment)
            addToBackStack(null)
            commit()
        }

CodePudding user response:

If you redirect one fragment to another fragment without destroying previous fragment use this line of code :-

   findNavController.navigate((R.id.calendarioFragment)Yourfragment id in navigation graph)
  • Related