android 导航控制器返回一级
android Navigation controller go back one level
我正在使用导航控制器。
我想知道我是否应该添加一个额外的目的地才能返回?或者有一个内置调用可以做到这一点。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_sign_up, container, false).apply {
val navController = NavHostFragment.findNavController(this@SignUpFragment)
this.btnSignUp.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_homeActivity)
}
this.btnGotoLogin.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_loginFragment)
}
this.back_to_auth_selection.setOnClickListener {
// should I add another action ?
}
}
}
this.back_to_auth_selection.setOnClickListener
{
activity?.onBackPressed()
}
您还可以在除主片段之外的每个片段上包含一个 Up-button
要在主要 activity`
中执行此操作
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// use nav controller to add Up-Button to the app
val navController = this.findNavController(R.id.nav_host_fragment)
// link the navigation controller to the app bar
NavigationUI.setupActionBarWithNavController(this, navController)
}
//override onSupportNavigateUp() to call navigateUp() in the navigation controller
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_host_fragment)
return navController.navigateUp()
}
}`
抱歉,我的代码是 Kotlin 语言
我正在使用导航控制器。
我想知道我是否应该添加一个额外的目的地才能返回?或者有一个内置调用可以做到这一点。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_sign_up, container, false).apply {
val navController = NavHostFragment.findNavController(this@SignUpFragment)
this.btnSignUp.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_homeActivity)
}
this.btnGotoLogin.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_loginFragment)
}
this.back_to_auth_selection.setOnClickListener {
// should I add another action ?
}
}
}
this.back_to_auth_selection.setOnClickListener
{
activity?.onBackPressed()
}
您还可以在除主片段之外的每个片段上包含一个 Up-button
要在主要 activity`
中执行此操作class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// use nav controller to add Up-Button to the app
val navController = this.findNavController(R.id.nav_host_fragment)
// link the navigation controller to the app bar
NavigationUI.setupActionBarWithNavController(this, navController)
}
//override onSupportNavigateUp() to call navigateUp() in the navigation controller
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_host_fragment)
return navController.navigateUp()
}
}`
抱歉,我的代码是 Kotlin 语言