优化plus
This commit is contained in:
@@ -6,6 +6,7 @@ import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
@@ -28,6 +29,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private var currentTabTag = TAB_HOME
|
||||
private var pendingTabAfterLogin: String? = null
|
||||
private var isSwitchingTab = false
|
||||
private var pendingTabSwitchTag: String? = null
|
||||
|
||||
private val protectedTabs = setOf(
|
||||
R.id.shop_graph,
|
||||
@@ -360,32 +363,58 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
val fm = supportFragmentManager
|
||||
if (fm.isStateSaved) return
|
||||
if (isSwitchingTab) {
|
||||
pendingTabSwitchTag = targetTag
|
||||
return
|
||||
}
|
||||
|
||||
val targetHost = when (targetTag) {
|
||||
TAB_SHOP -> shopHost
|
||||
TAB_MINE -> mineHost
|
||||
else -> homeHost
|
||||
}
|
||||
val currentHost = currentTabHost
|
||||
|
||||
currentTabTag = targetTag
|
||||
isSwitchingTab = true
|
||||
|
||||
fm.beginTransaction()
|
||||
val transaction = fm.beginTransaction()
|
||||
.setReorderingAllowed(true)
|
||||
.hide(homeHost)
|
||||
.hide(shopHost)
|
||||
.hide(mineHost)
|
||||
.also { ft ->
|
||||
when (targetTag) {
|
||||
TAB_SHOP -> ft.show(shopHost)
|
||||
TAB_MINE -> ft.show(mineHost)
|
||||
else -> ft.show(homeHost)
|
||||
|
||||
if (force) {
|
||||
transaction
|
||||
.hide(homeHost)
|
||||
.hide(shopHost)
|
||||
.hide(mineHost)
|
||||
.show(targetHost)
|
||||
} else if (currentHost != targetHost) {
|
||||
transaction
|
||||
.hide(currentHost)
|
||||
.show(targetHost)
|
||||
}
|
||||
|
||||
transaction
|
||||
.setMaxLifecycle(homeHost, if (targetHost == homeHost) Lifecycle.State.RESUMED else Lifecycle.State.STARTED)
|
||||
.setMaxLifecycle(shopHost, if (targetHost == shopHost) Lifecycle.State.RESUMED else Lifecycle.State.STARTED)
|
||||
.setMaxLifecycle(mineHost, if (targetHost == mineHost) Lifecycle.State.RESUMED else Lifecycle.State.STARTED)
|
||||
.setPrimaryNavigationFragment(targetHost)
|
||||
.runOnCommit {
|
||||
isSwitchingTab = false
|
||||
updateBottomNavVisibility()
|
||||
|
||||
if (!force) {
|
||||
currentTabNavController.currentDestination?.id?.let { destId ->
|
||||
reportPageView(source = "switch_tab", destId = destId)
|
||||
}
|
||||
}
|
||||
|
||||
val pendingTag = pendingTabSwitchTag
|
||||
pendingTabSwitchTag = null
|
||||
if (pendingTag != null && pendingTag != currentTabTag) {
|
||||
switchTab(pendingTag)
|
||||
}
|
||||
}
|
||||
.commit()
|
||||
|
||||
// ✅ 关键:hide/show 切 tab 不会触发 destinationChanged,所以手动刷新
|
||||
bottomNav.post { updateBottomNavVisibility() }
|
||||
|
||||
// ✅ 新增:切 tab 后补一次路由上报(不改变其它逻辑)
|
||||
if (!force) {
|
||||
currentTabNavController.currentDestination?.id?.let { destId ->
|
||||
reportPageView(source = "switch_tab", destId = destId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 打开全局页(login/recharge等) */
|
||||
|
||||
Reference in New Issue
Block a user