关闭权限并返回应用程序时,Bottomnavigationview不起作用

时间:2019-03-12 07:53:37

标签: android android-fragments kotlin bottomnavigationview

如果我手动关闭电话权限,该应用将重新启动。

Bottomnavigationview在应用程序重新启动完成后没有响应。

按下按钮不会更改Fragment

但是当时的Fragment已恢复。

class MyActivity : AppCompatActivity(), OnMyRedirectRequest {

private val uiNavBottom by lazy(LazyThreadSafetyMode.NONE) { findViewById<BottomNavigationView>(R.id.nav_bottom) }
private val eventNavigate = BottomNavigationView.OnNavigationItemSelectedListener { onRedirect(it.itemId) }
private lateinit var vmMyModel: MyViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    vmMyModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
    uiNavBottom.setOnNavigationItemSelectedListener(eventNavigate)
    savedInstanceState ?: onRedirect(R.id.home)
}

override fun onRedirect(pageId: Int, options: Bundle?): Boolean {
    var selectedFragment: Fragment? = null
    val manager: FragmentManager = supportFragmentManager

    when (pageId) {
        R.id.home -> {
            selectedFragment = if (vmMyModel.selectedPage != 0) HomePage.newInstance() else null
            vmMyModel.selectedPage = 0
        }
        R.id.owner -> {
            selectedFragment = if (vmMyModel.selectedPage != 1) OwnerPage.newInstance() else null
            vmMyModel.selectedPage = 1
        }
        R.id.setting -> {
            selectedFragment = if (vmMyModel.selectedPage != 1) SettingPage.newInstance() else null
            vmMyModel.selectedPage = 1
        }
        else -> return false
    }
    selectedFragment?.let { fragment ->
        try {
            manager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.fl_page, fragment)
                    .commitNow()
        } catch (ex: IllegalStateException) {
            Log.e("ex", ex.toString())
        }
        return true
    }
    return false
    }
}

0 个答案:

没有答案