单击BackPressed按钮后,AppBarLayout无法显示

时间:2020-08-03 06:36:11

标签: android kotlin android-appbarlayout

我想在用户单击onBackPressed按钮时显示AppBarLayout。我怎样才能做到这一点? 这是我的代码

片段详细信息帖子

private fun toolBarListener(view: View){
    val toolbar: Toolbar = view.findViewById(R.id.tbDetailPost) as Toolbar

    toolbar.setNavigationOnClickListener {

        if (context is GeneralActivity){
            (context as GeneralActivity).setAppBarVisible()
        }else if(context is ProfileActivity){
            (context as ProfileActivity).setAppBarVisible()
        }else if (context is NotificationActivity){
            (context as NotificationActivity).setAppBarVisible()
        }
        activity?.onBackPressed()
    }
}

这是函数setAppBarVisible()

fun setAppBarVisible() {
    appBarLayout.visibility = View.VISIBLE
}

1 个答案:

答案 0 :(得分:0)

覆盖onBackPressed()方法,然后像这样调用setAppBarVisible()

override fun onBackPressed() {
    super.onBackPressed()
    setAppBarVisible()
}