将片段重定向到另一个片段
Redirect a Fragment to another Fragment
我正在研究很多关于 Kotlin 的知识,并且我有一个项目要完成。所以,我有一些碎片和一个 Activity。
左侧菜单中的所有操作都有效,好的。但是在 CalendarioFragment 中,我需要在用户按下按钮时重定向到另一个片段。现在,我不知道该怎么做。我正在使用 Nav Graph,两者都启用了 navhost。
<?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>
我的主要Activity:
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()
}
}
我尝试将 supportFragmentManager 放在 MainActivity 上,但这样一来,应用程序就会在初始化时崩溃。有没有办法从本地 Fragment 重定向到另一个 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
}
主要活动
var fragment: Fragment? = null
fragment = CalendarioFragment()
if (fragment != null) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.calendarioFragment, fragment)
transaction.commit()
}
如果您将一个片段重定向到另一个片段而不破坏前一个片段,请使用这行代码:-
findNavController.navigate((R.id.calendarioFragment)Yourfragment id in navigation graph)
我正在研究很多关于 Kotlin 的知识,并且我有一个项目要完成。所以,我有一些碎片和一个 Activity。 左侧菜单中的所有操作都有效,好的。但是在 CalendarioFragment 中,我需要在用户按下按钮时重定向到另一个片段。现在,我不知道该怎么做。我正在使用 Nav Graph,两者都启用了 navhost。
<?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>
我的主要Activity:
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()
}
}
我尝试将 supportFragmentManager 放在 MainActivity 上,但这样一来,应用程序就会在初始化时崩溃。有没有办法从本地 Fragment 重定向到另一个 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
}
主要活动
var fragment: Fragment? = null
fragment = CalendarioFragment()
if (fragment != null) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.calendarioFragment, fragment)
transaction.commit()
}
如果您将一个片段重定向到另一个片段而不破坏前一个片段,请使用这行代码:-
findNavController.navigate((R.id.calendarioFragment)Yourfragment id in navigation graph)