是否可以创建一个Navigation Fragment
来包含我的导航后退按钮单击逻辑。
Navigation Fragment
继承。
我是Kotlin开发的新手。如您在SigninFragment
下面放大视图所示,我不确定如何获取父Navigation Fragment
中的视图和后退按钮的引用
class SigninFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
// Navigation back button logic
var headerBackButton = view.findViewById<ImageButton>(R.id.headerBackButton)
headerBackButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this@SignInFragment)
navController.navigateUp()
}
return view
}
}
答案 0 :(得分:1)
我不确定我是否能解决您的问题,但这可以解决问题吗?
open class NavigationFragment() : Fragment() {
fun asignNavigationBackClickListener(backButton: View) {
backButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this@NavigationFragment)
navController.navigateUp()
}
}
}
class SigninFragment : NavigationFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
asignNavigationBackClickListener(view.findViewById(R.id.headerBackButton))
return view
}
}
答案 1 :(得分:0)
我认为您可以使用以下代码返回上一个活动:
headerBackButton.setOnClickListener {
finish()
}