-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update NavigatorImpl.pop() to invoke onBackPressedDispatcher (#264)
The current back solution seems to work when tapping the system back button or triggering the back gesture. However, manual calls to Navigator.pop (triggered by an on screen back button) when viewing the root backstack record will crash the app. This happens purposely as we don't want to allow an empty backstack. To resolve this, NavigatorImpl has been updated to invoke a onBackPressedDispatcher whenever pop() is called while on the root backstack record. Co-authored-by: Zac Sweers <pandanomic@gmail.com>
- Loading branch information
Showing
6 changed files
with
145 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
samples/star/src/main/kotlin/com/slack/circuit/star/common/NavigableTopBarScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2022 Slack Technologies, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.slack.circuit.star.common | ||
|
||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Close | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
|
||
@Composable | ||
fun BackPressNavIcon( | ||
modifier: Modifier = Modifier, | ||
iconButtonContent: @Composable () -> Unit = { ClosedIconImage() }, | ||
) { | ||
val onBackPressedDispatcher = | ||
LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher | ||
?: error("No local LocalOnBackPressedDispatcherOwner found.") | ||
IconButton(modifier = modifier, onClick = onBackPressedDispatcher::onBackPressed) { | ||
iconButtonContent() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ClosedIconImage(modifier: Modifier = Modifier) { | ||
Image( | ||
modifier = modifier, | ||
painter = rememberVectorPainter(image = Icons.Filled.Close), | ||
contentDescription = "Close", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters