diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index bcd3fd017..81b7888f8 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.main import android.os.Bundle +import android.view.KeyEvent import android.view.MenuItem import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager @@ -34,6 +35,8 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) + private var pagePosition = 0 + override fun onActivityCreated(savedInstanceState: Bundle?) { ATH.applyEdgeEffectColor(view_pager_main) ATH.applyBottomNavigationColor(bottom_navigation_view) @@ -80,9 +83,26 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } override fun onPageSelected(position: Int) { + pagePosition = position bottom_navigation_view.menu.getItem(position).isChecked = true } + override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { + event?.let { + when (keyCode) { + KeyEvent.KEYCODE_BACK -> if ( + pagePosition != 0 + && event.isTracking + && !event.isCanceled + ) { + view_pager_main.currentItem = 0 + return true + } + } + } + return super.onKeyUp(keyCode, event) + } + private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { @@ -98,6 +118,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun getCount(): Int { return 4 } + } override fun observeLiveBus() {