Skip to content

Commit e529415

Browse files
Merge pull request #3 from michalchudziak/dratwas/autolink
[Android] back button for activity and autolink
2 parents 5e0a2ad + bbca61e commit e529415

File tree

7 files changed

+76
-9
lines changed

7 files changed

+76
-9
lines changed

android/src/main/java/com/callstack/reactnativebrownfield/ReactNativeActivity.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, Perm
7777
}
7878

7979
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
80+
if(keyCode == KeyEvent.KEYCODE_BACK) {
81+
onBackPressed()
82+
return true
83+
}
8084
if (BridgeManager.shared.reactNativeHost.hasInstance()
8185
&& BridgeManager.shared.reactNativeHost.useDeveloperSupport) {
8286
if (keyCode == KeyEvent.KEYCODE_MENU) {
@@ -105,10 +109,11 @@ class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, Perm
105109
}
106110

107111
override fun onBackPressed() {
108-
super.invokeDefaultOnBackPressed()
109-
// if (BridgeManager.shared.reactNativeHost.hasInstance()) {
110-
// BridgeManager.shared.reactNativeHost.reactInstanceManager.onBackPressed()
111-
// }
112+
if(ReactNativeBrownfieldModule.shouldPopToNative) {
113+
super.invokeDefaultOnBackPressed()
114+
} else if (BridgeManager.shared.reactNativeHost.hasInstance()) {
115+
BridgeManager.shared.reactNativeHost.reactInstanceManager.onBackPressed()
116+
}
112117
}
113118

114119
override fun invokeDefaultOnBackPressed() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.callstack.reactnativebrownfield
2+
3+
import com.facebook.react.bridge.ReactApplicationContext
4+
import com.facebook.react.bridge.ReactContextBaseJavaModule
5+
import com.facebook.react.bridge.ReactMethod
6+
7+
8+
class ReactNativeBrownfieldModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
9+
companion object {
10+
var shouldPopToNative: Boolean = false
11+
}
12+
13+
@ReactMethod
14+
fun popToNative(test: Boolean) {
15+
shouldPopToNative = true
16+
onBackPressed()
17+
}
18+
19+
@ReactMethod
20+
fun setPopGestureRecognizer(isFirstRoute: Boolean) {
21+
shouldPopToNative = isFirstRoute
22+
}
23+
24+
private fun onBackPressed() {
25+
reactApplicationContext.currentActivity?.runOnUiThread {
26+
reactApplicationContext.currentActivity?.onBackPressed()
27+
}
28+
}
29+
30+
override fun getName(): String {
31+
return "ReactNativeBrownfield"
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.callstack.reactnativebrownfield
2+
3+
import android.view.View
4+
import java.util.Collections
5+
6+
import com.facebook.react.ReactPackage
7+
import com.facebook.react.bridge.NativeModule
8+
import com.facebook.react.bridge.ReactApplicationContext
9+
import com.facebook.react.uimanager.ViewManager
10+
import com.facebook.react.uimanager.ReactShadowNode
11+
12+
13+
class ReactNativeBrownfieldPackage : ReactPackage {
14+
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<ViewManager<View, ReactShadowNode<*>>> {
15+
return Collections.emptyList()
16+
}
17+
18+
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
19+
val modules = ArrayList<NativeModule>()
20+
modules.add(ReactNativeBrownfieldModule(reactContext))
21+
return modules
22+
}
23+
}

example/native/android/app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ android {
3232
}
3333

3434
dependencies {
35-
implementation project(':react-native-brownfield')
3635
implementation fileTree(dir: 'libs', include: ['*.jar'])
3736
implementation "com.facebook.react:react-native:+"
3837
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
apply from: file("../../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle");
22
applyNativeModulesSettingsGradle(settings, "../../..")
33

4-
include ':app', ':react-native-brownfield'
5-
6-
project(':react-native-brownfield').projectDir = new File(rootProject.projectDir, '../../../android')
4+
include ':app'

example/native/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HomeScreen extends React.Component {
4747
<Button
4848
onPress={() => {
4949
if (isFirstRoute) {
50-
NativeModules.ReactNativeBrownfield.popToNative(true);
50+
NativeModules.ReactNativeBrownfield.popToNative(true);
5151
} else {
5252
this.props.navigation.goBack();
5353
}

react-native.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const root = process.cwd();
2+
3+
module.exports = {
4+
dependencies: {
5+
'react-native-brownfield': {
6+
root,
7+
},
8+
},
9+
};

0 commit comments

Comments
 (0)