fix: 解绑处理移到MainActivity,用全局action从任何页面跳转
HomeFragment在子页面时viewLifecycleOwner被销毁,collect停止, 收不到解绑消息。改为Activity级别处理+全局导航action,彻底解决。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import androidx.navigation.findNavController
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.xiaoqu.watch.databinding.ActivityMainBinding
|
||||
@@ -112,17 +113,32 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
// ===== MQTT 新任务处理 =====
|
||||
|
||||
/** 监听 MQTT type=1 → 通知管理器处理 → 横幅 + NewTaskArrived 事件 */
|
||||
/** 监听 MQTT 消息(Activity 级别,不受 Fragment 生命周期影响) */
|
||||
private fun observeMqttMessages() {
|
||||
activityScope.launch {
|
||||
eventBus.events.collect { event ->
|
||||
when (event) {
|
||||
is AppEvent.MqttMessageReceived -> {
|
||||
if (event.type == 1) {
|
||||
notificationManager.onNewTaskMessage(event.rawJson)
|
||||
val count = notificationManager.pendingCount
|
||||
if (count > 0) {
|
||||
notificationBanner.show(count)
|
||||
when (event.type) {
|
||||
1 -> {
|
||||
// 新任务通知
|
||||
notificationManager.onNewTaskMessage(event.rawJson)
|
||||
val count = notificationManager.pendingCount
|
||||
if (count > 0) {
|
||||
notificationBanner.show(count)
|
||||
}
|
||||
}
|
||||
3 -> {
|
||||
// 解绑 → 清除数据 → 跳绑定页(从任何页面都能跳)
|
||||
Timber.d("MainActivity: 收到解绑消息")
|
||||
bluetoothScanManager.stop()
|
||||
userPrefs.clear()
|
||||
try {
|
||||
findNavController(R.id.nav_host_fragment)
|
||||
.navigate(R.id.action_global_to_bind)
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e, "MainActivity: 解绑导航异常")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -626,25 +626,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
||||
Timber.d("首页: 收到日常动态")
|
||||
fetchStatistics()
|
||||
}
|
||||
3 -> {
|
||||
// 解绑 → 停止蓝牙 → 清除数据 → 跳绑定页
|
||||
Timber.d("首页: 收到解绑消息")
|
||||
bluetoothScanManager.stop()
|
||||
userPrefs.clear()
|
||||
try {
|
||||
// 确保当前在 homeFragment 才能导航
|
||||
val navController = findNavController()
|
||||
if (navController.currentDestination?.id == R.id.homeFragment) {
|
||||
navController.navigate(R.id.action_home_to_bind)
|
||||
} else {
|
||||
// 在子页面(任务列表/详情),先弹回首页再跳绑定
|
||||
navController.popBackStack(R.id.homeFragment, false)
|
||||
navController.navigate(R.id.action_home_to_bind)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e, "首页: 解绑导航异常")
|
||||
}
|
||||
}
|
||||
// type=3 解绑已移到 MainActivity 处理(Activity 级别,不受 Fragment 生命周期影响)
|
||||
4 -> {
|
||||
// 系统参数变更(蓝牙/NFC 配置)
|
||||
Timber.d("首页: 收到系统参数变更")
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
android:id="@+id/nav_main"
|
||||
app:startDestination="@id/splashFragment">
|
||||
|
||||
<!-- 全局 action:解绑时从任何页面跳转到绑定页(清空回退栈) -->
|
||||
<action android:id="@+id/action_global_to_bind"
|
||||
app:destination="@id/bindFragment"
|
||||
app:popUpTo="@id/nav_main" app:popUpToInclusive="true" />
|
||||
|
||||
<!-- 启动分发页 -->
|
||||
<fragment
|
||||
android:id="@+id/splashFragment"
|
||||
|
||||
Reference in New Issue
Block a user