Skip to content

Commit 7f975eb

Browse files
committed
Add BackHandlerView hosting a Compose BackHandler
1 parent ac3204c commit 7f975eb

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.pureswift.swiftandroid
2+
3+
import android.content.Context
4+
import android.widget.FrameLayout
5+
import androidx.activity.compose.BackHandler
6+
import androidx.compose.runtime.getValue
7+
import androidx.compose.runtime.mutableStateOf
8+
import androidx.compose.runtime.setValue
9+
import androidx.compose.ui.platform.ComposeView
10+
11+
// Hosts a Jetpack Compose `BackHandler` to intercept the system back button and forward it to
12+
// Swift via `onBack()`. Renders no visible UI of its own; used only for its back-press interception.
13+
class BackHandlerView(context: Context, val callback: SwiftObject) : FrameLayout(context) {
14+
15+
private var handlerEnabled by mutableStateOf(true)
16+
17+
init {
18+
val composeView = ComposeView(context)
19+
composeView.setContent {
20+
BackHandler(enabled = handlerEnabled) {
21+
onBack()
22+
}
23+
}
24+
addView(composeView)
25+
}
26+
27+
fun setBackHandlerEnabled(value: Boolean) {
28+
handlerEnabled = value
29+
}
30+
31+
external fun onBack()
32+
}

0 commit comments

Comments
 (0)