美文网首页Android开发Android开发Android开发经验谈
Fragment监听物理返回键onBackPressed

Fragment监听物理返回键onBackPressed

作者: Jack_e002 | 来源:发表于2019-04-03 14:59 被阅读16次

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方法处理自己的业务逻辑跳转。

相关文章

网友评论

    本文标题:Fragment监听物理返回键onBackPressed

    本文链接:https://www.haomeiwen.com/subject/zkhzbqtx.html