I'm new to Android, and I'm encountering an issue with navigation and the back stack. Here's the situation I want to achieve:
In my app, users can modify certain settings that require approval through a push notification. Here's the desired flow:
- The user opens my app, and the login screen appears (which is defined as the launcher screen in the manifest).
- After logging in, the user navigates to the settings screen and makes a change, such as updating their email.
- At this point, a push notification arrives. When the user clicks on it, a new activity is opened (using the flags Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK).
- On the new activity, the user is required to approve the change. After successful approval, a close button is displayed. When the user clicks the close button, they should return to the previous state of the settings screen in my app (resume to the previous state).
The code of the closing button for navigating back in stack works fine on Android versions higher than 11. However, I've noticed a different behavior on Android 11, as mentioned in the official Android documentation. In my case the Launcher Activity is called, and it places the Login Activity on top of it. When I navigate back, it displays the correct Settings Screen. But i want to display the Settings screen right away.
I'm currently using the following code in the onCloseButtonClick method of the PushActivity:
`closeButton.setOnClickListener {
val intent = requireContext().packageManager
.getLaunchIntentForPackage(requireContext().packageName)
?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
?: return
runCatching {
startActivity(intent)
}.onFailure {}
requireActivity().finish()
}`
Does anyone know what changes I need to make so that I can directly return to the Settings Activity?