|
1 | 1 | package com.sampleapp |
2 | 2 |
|
3 | | -import com.facebook.react.ReactActivity |
4 | | -import com.facebook.react.ReactActivityDelegate |
5 | | -import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled |
6 | | -import com.facebook.react.defaults.DefaultReactActivityDelegate |
7 | | - |
8 | | -import android.os.Bundle |
9 | 3 | import android.os.Build |
| 4 | +import android.os.Bundle |
10 | 5 | import android.view.View |
| 6 | +import androidx.core.graphics.Insets |
11 | 7 | import androidx.core.view.ViewCompat |
12 | 8 | import androidx.core.view.WindowInsetsCompat |
13 | 9 | import androidx.core.view.updatePadding |
| 10 | +import com.facebook.react.ReactActivity |
| 11 | +import com.facebook.react.ReactActivityDelegate |
| 12 | +import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled |
| 13 | +import com.facebook.react.defaults.DefaultReactActivityDelegate |
14 | 14 |
|
15 | 15 | class MainActivity : ReactActivity() { |
16 | 16 |
|
17 | | - override fun onCreate(savedInstanceState: Bundle?) { |
18 | | - super.onCreate(null) |
19 | | - |
20 | | - if (Build.VERSION.SDK_INT >= 35) { |
21 | | - val rootView = findViewById<View>(android.R.id.content) |
22 | | - |
23 | | - |
24 | | - ViewCompat.setOnApplyWindowInsetsListener(rootView) { view, insets -> |
25 | | - val bars = insets.getInsets( |
26 | | - WindowInsetsCompat.Type.systemBars() |
27 | | - or WindowInsetsCompat.Type.displayCutout() |
28 | | - or WindowInsetsCompat.Type.ime() // adding the ime's height |
29 | | - ) |
30 | | - rootView.updatePadding( |
31 | | - left = bars.left, |
32 | | - top = bars.top, |
33 | | - right = bars.right, |
34 | | - bottom = bars.bottom |
35 | | - ) |
36 | | - WindowInsetsCompat.CONSUMED |
37 | | - } |
38 | | - } |
| 17 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 18 | + super.onCreate(null) |
| 19 | + |
| 20 | + if (Build.VERSION.SDK_INT >= 35) { |
| 21 | + val rootView = findViewById<View>(android.R.id.content) |
| 22 | + |
| 23 | + // Keep the original padding so we can restore it when IME closes |
| 24 | + val initial = Insets.of( |
| 25 | + rootView.paddingLeft, |
| 26 | + rootView.paddingTop, |
| 27 | + rootView.paddingRight, |
| 28 | + rootView.paddingBottom |
| 29 | + ) |
| 30 | + |
| 31 | + ViewCompat.setOnApplyWindowInsetsListener(rootView) { v, insets -> |
| 32 | + // Only apply IME (keyboard) to avoid breaking safe-area libs or double-padding system bars. |
| 33 | + val ime = insets.getInsets(WindowInsetsCompat.Type.ime()) |
| 34 | + |
| 35 | + v.updatePadding( |
| 36 | + left = initial.left, |
| 37 | + top = initial.top, |
| 38 | + right = initial.right, |
| 39 | + bottom = initial.bottom + ime.bottom |
| 40 | + ) |
| 41 | + |
| 42 | + // IMPORTANT: don't consume — allow insets to propagate to RN & safe-area-context |
| 43 | + insets |
39 | 44 | } |
40 | | - /** |
41 | | - * Returns the name of the main component registered from JavaScript. This is used to schedule |
42 | | - * rendering of the component. |
43 | | - */ |
44 | | - override fun getMainComponentName(): String = "SampleApp" |
45 | | - |
46 | | - /** |
47 | | - * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate] |
48 | | - * which allows you to enable New Architecture with a single boolean flags [fabricEnabled] |
49 | | - */ |
50 | | - override fun createReactActivityDelegate(): ReactActivityDelegate = |
51 | | - DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) |
| 45 | + |
| 46 | + // Make sure we get an initial insets dispatch |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + override fun getMainComponentName(): String = "SampleApp" |
| 51 | + |
| 52 | + override fun createReactActivityDelegate(): ReactActivityDelegate = |
| 53 | + DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) |
52 | 54 | } |
0 commit comments