Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate BackPress to OnBackPressedCallback #2945

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions ground/src/main/java/com/google/android/ground/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -85,6 +86,18 @@
lifecycleScope.launch {
viewModel.navigationRequests.filterNotNull().collect { updateUi(binding.root, it) }
}

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (!dispatchBackPressed()) {
isEnabled = false
onBackPressedDispatcher.onBackPressed()

Check warning on line 96 in ground/src/main/java/com/google/android/ground/MainActivity.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/MainActivity.kt#L95-L96

Added lines #L95 - L96 were not covered by tests
}
}

Check warning on line 98 in ground/src/main/java/com/google/android/ground/MainActivity.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/MainActivity.kt#L98

Added line #L98 was not covered by tests
},
)
}

private fun updateUi(viewGroup: ViewGroup, uiState: MainUiState) {
Expand Down Expand Up @@ -182,11 +195,6 @@
}
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!dispatchBackPressed()) super.onBackPressed()
}

private fun dispatchBackPressed(): Boolean {
val fragmentManager = navHostFragment.childFragmentManager
val currentFragment = fragmentManager.findFragmentById(R.id.nav_host_fragment)
Expand Down
Loading