From aa88ff9fb535f2bc229f2daaed04da772bb97aba Mon Sep 17 00:00:00 2001 From: Dennis Arndt Date: Wed, 4 Dec 2024 17:20:17 +0000 Subject: [PATCH] fix new Device not loading correctly when accessing a Device through a Notification while another device is already opened --- .../ui/TrackingNotificationActivity.kt | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt b/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt index 59f07d7e..6689304d 100644 --- a/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt +++ b/app/src/main/java/de/seemoo/at_tracking_detection/ui/TrackingNotificationActivity.kt @@ -1,5 +1,6 @@ package de.seemoo.at_tracking_detection.ui +import android.content.Intent import android.os.Bundle import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity @@ -23,31 +24,7 @@ class TrackingNotificationActivity : AppCompatActivity() { val navHostFragment = supportFragmentManager.findFragmentById(R.id.tracking_host_fragment) as NavHostFragment navController = navHostFragment.navController - val deviceAddress = intent.getStringExtra("deviceAddress") - val deviceTypeAsString = intent.getStringExtra("deviceTypeAsString") ?: "UNKNOWN" - val notificationId = intent.getIntExtra("notificationId", -1) - Timber.d("Tracking Activity with device $deviceAddress and notification $notificationId started!") - - if (deviceAddress == null) { - Timber.e("Device address is needed! Going home...") - this.onSupportNavigateUp() - } else { - // Workaround: Somehow not possible to use getString with deviceAddress as an Argument - var getTitle = getString(R.string.title_devices_tracking) - getTitle = getTitle.replace("{deviceAddress}", deviceAddress.toString()) - supportActionBar?.title = getTitle - - val args = TrackingFragmentArgs( - deviceAddress = deviceAddress, - deviceTypeAsString = deviceTypeAsString, - notificationId = notificationId - ).toBundle() - navController.setGraph(R.navigation.main_navigation) - val navOptions = NavOptions.Builder() - // .setPopUpTo(R.id.navigation_dashboard, true) - .build() - navController.navigate(R.id.trackingFragment, args, navOptions) - } + navigateToTrackingFragment() onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { @@ -86,4 +63,39 @@ class TrackingNotificationActivity : AppCompatActivity() { } } } + + override fun onNewIntent(intent: Intent) { + super.onNewIntent(intent) + setIntent(intent) + + navigateToTrackingFragment() + } + + private fun navigateToTrackingFragment() { + val deviceAddress = intent.getStringExtra("deviceAddress") + val deviceTypeAsString = intent.getStringExtra("deviceTypeAsString") ?: "UNKNOWN" + val notificationId = intent.getIntExtra("notificationId", -1) + Timber.d("Tracking Activity with device $deviceAddress and notification $notificationId started!") + + if (deviceAddress == null) { + Timber.e("Device address is needed! Going home...") + this.onSupportNavigateUp() + } else { + // Workaround: Somehow not possible to use getString with deviceAddress as an Argument + var getTitle = getString(R.string.title_devices_tracking) + getTitle = getTitle.replace("{deviceAddress}", deviceAddress.toString()) + supportActionBar?.title = getTitle + + val args = TrackingFragmentArgs( + deviceAddress = deviceAddress, + deviceTypeAsString = deviceTypeAsString, + notificationId = notificationId + ).toBundle() + navController.setGraph(R.navigation.main_navigation) + val navOptions = NavOptions.Builder() + // .setPopUpTo(R.id.navigation_dashboard, true) + .build() + navController.navigate(R.id.trackingFragment, args, navOptions) + } + } } \ No newline at end of file