18034727885
2026-06-23 56fedc881e978953c3b0636b93c6e25852efa190
app/src/main/java/com/xw/yunwei/ui/MainActivity.kt
@@ -2,9 +2,11 @@
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.lifecycleScope
import com.amap.api.location.AMapLocation
import com.blankj.utilcode.util.AppUtils
@@ -42,6 +44,27 @@
        MineFragment::class.java.name
    )
    lateinit var gpsHelper: GpsHelper
    /**
     * 进程被杀重建时(如后台禁用运行时权限触发进程死亡),
     * ViewModel 会被重建、selectIndex 丢失并回退到默认 0。
     * 此处在 ViewModel 创建之后、setObservable 注册观察者之前,
     * 先把上次保存的 index 恢复回去,避免重建后底部导航错乱。
     */
    override fun myCreate(savedInstanceState: Bundle?) {
        super.myCreate(savedInstanceState)
        savedInstanceState?.let {
            if (it.containsKey(KEY_SELECT_INDEX)) {
                viewModel.selectIndex.value = it.getInt(KEY_SELECT_INDEX)
            }
        }
    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        viewModel.selectIndex.value?.let { outState.putInt(KEY_SELECT_INDEX, it) }
    }
    override fun init() {
        super.init()
        val user = MMKVManager.getInstance().getModel<UserInfo>(KeyPreferences.USER)
@@ -76,15 +99,7 @@
        super.setObservable()
        viewModel.selectIndex.observe(this) {
            showFragment(it)
            if (viewModel.taskPermissions.value == true) {
                binding.navView.visibility = if (viewModel.workPermissions.value == true) {
                    if (it == 1) View.GONE else View.VISIBLE
                } else {
                    if (it == 0) View.GONE else View.VISIBLE
                }
            } else {
                binding.navView.visibility = View.VISIBLE
            }
            updateNavViewVisibility(it)
        }
        MyApp.permissions.observe(this) {
            viewModel.workPermissions.value = MyApp.findPermissionByName("工作台")?.visible == "0"
@@ -107,7 +122,27 @@
                    currentIndex = 2
                }
                viewModel.changeSelectFragmentIndex(currentIndex)
                // 权限可能在 index 不变的情况下变化,需主动重算底部主导航可见性,
                // 否则 selectIndex 不发射时 navView 可见性无法被纠正(双 Tab bar 叠加根因之一)
                updateNavViewVisibility(currentIndex)
            }
        }
    }
    /**
     * 重新计算 Activity 底部主导航(工作台/任务中心/我的)的可见性。
     * 进入任务中心(index==1)时隐藏主导航,仅展示 TaskFragment 自带的底部栏。
     * 抽成独立方法,使其在 index 变化与权限变化时都能被触发。
     */
    private fun updateNavViewVisibility(index: Int) {
        binding.navView.visibility = if (viewModel.taskPermissions.value == true) {
            if (viewModel.workPermissions.value == true) {
                if (index == 1) View.GONE else View.VISIBLE
            } else {
                if (index == 0) View.GONE else View.VISIBLE
            }
        } else {
            View.VISIBLE
        }
    }
@@ -125,7 +160,7 @@
            if (it.forceUpdateFlag == 1) {
                UpdateManager.instance!!
                    .setContext(this@MainActivity)
                    .apkUrl(it.androidFile ?: "")
                    .apkUrl(it.getDownloadUrl() ?: "")
                    .updateContent(it.updateDescription ?: "")
                    .updateVersionCode(it.versionCode ?: 1)
                    .updateVersionName(it.version ?: "1.0")
@@ -184,6 +219,10 @@
    private fun showFragment(from: Fragment?, to: Fragment, tag: String) {
        val transaction = supportFragmentManager.beginTransaction()
        if (from == null) {
            // 进程重建后 mCurrentFragment 丢失,FragmentManager 会自动恢复之前 add 过的
            // Fragment 并保留其显隐状态。此处主动隐藏除目标外的其他 Fragment,
            // 避免任务中心与工作台等 Fragment 叠加导致出现两套底部 Tab bar。
            hideOtherFragments(transaction, tag)
            if (to.isAdded) {
                transaction.show(to)
            } else {
@@ -198,6 +237,17 @@
        }
        transaction.commit()
        mCurrentFragment = to
    }
    /**
     * 隐藏除 currentTag 之外、已被 FragmentManager 恢复/添加的导航 Fragment,
     * 保证同一时刻只有一个 Fragment 可见。
     */
    private fun hideOtherFragments(transaction: FragmentTransaction, currentTag: String) {
        for (name in fragmentNames) {
            if (name == currentTag) continue
            supportFragmentManager.findFragmentByTag(name)?.let { transaction.hide(it) }
        }
    }
    override fun onResume() {
@@ -227,4 +277,8 @@
    override fun location(pLocation: AMapLocation?) {
    }
    companion object {
        private const val KEY_SELECT_INDEX = "key_select_index"
    }
}