BaseFragment.kt
/**
* 监听返回键
* @return true:返回,false:阻止返回
*/
open fun onBackPressed(): Boolean {
return true
}
Activity重写onBackPressed
/**
* 返回键监听
*/
override fun onBackPressed() {
if (supportFragmentManager.backStackEntryCount == 0) {
super.onBackPressed()
return
}
val currentFragment = supportFragmentManager.fragments.last() as BaseFragment?
val backProcess = currentFragment?.onBackPressed()
if (backProcess != null && backProcess) {
super.onBackPressed()
}
}
子类继承BaseFragment可以重载onBackPressed方法处理自己的业务逻辑跳转。
网友评论