Shivam Kanodia
09/25/2023, 7:26 AMCan anyone help me with handling back buttons for android?. I a using Android Activity #decompose
, which contains a fragment, then fragment contains the RootComponent. When i am pressing back button on Android, its popping the fragment rather the popping a single component.
class RecceSectionsComponent(
componentContext: ComponentContext,
recceId: String,
sectionId: String,
sectionTitle: String,
recceGuide: Guide?,
projectId: String?,
storeFactory: StoreFactory,
private val output: (Output) -> Unit
) : ComponentContext by componentContext {
private val recceStore = instanceKeeper.getStore {
RecceDetailsStoreFactory(
storeFactory = storeFactory
).create()
}
private val backCallback = BackCallback {
onOutput(Output.NavigateBack)
}
init {
lifecycle.subscribe(object : Lifecycle.Callbacks {
override fun onResume() {
super.onResume()
}
override fun onStart() {
super.onStart()
}
})
backHandler.register(backCallback)
backCallback.isEnabled = true
}
val recceId = recceId
val sectionId = sectionId
val sectionTitle = sectionTitle
val recceGuide = recceGuide
val projectId = projectId
@OptIn(ExperimentalCoroutinesApi::class)
val state: StateFlow<RecceDetailsStore.State> = recceStore.stateFlow
fun onEvent(event: RecceDetailsStore.Intent) {
recceStore.accept(event)
}
fun onOutput(output: Output) {
output(output)
}
sealed class Output {
object NavigateBack : Output()
object Logout : Output()
data class OpenFullScreenComponent<T>(
val imagesList: List<T>,
val initialImageIndex: Int,
val openFrom: String,
val elementName: String? = null
) : Output()
}
}
Above is RecceSectionsComponent declared in RootComponent.
private val navigation = StackNavigation<Configuration>()
private val stack = childStack(
source = navigation,
initialConfiguration = getInitConfig(),
childFactory = ::createChild
)
Arkadii Ivanov
09/25/2023, 8:00 AMBackCallback
in your RecceSectionsComponent. Just pass handleBackButton = true
to childStack(...)
.