Issue
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:
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
}
Solution
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)
Answered By - Dheeraj Prajapat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.