一、添加MyIntentModule類,並繼承ReactContextBaseJavaModule實現其方法和構造函數。在該類中添加方法,注意:方法頭要加@ReactMethod
public
class
MyIntentModule
extends
ReactContextBaseJavaModule {
public
MyIntentModule(ReactApplicationContext reactContext) {
super
(reactContext);
}
@Override
public
String getName() {
return
"IntentMoudle"
;
}
//注意:記住getName方法中的命名名稱,JS中調用需要
@ReactMethod
public
void
startActivityFromJS(String name, String params){
try
{
Activity currentActivity = getCurrentActivity();
if
(
null
!=currentActivity){
Class toActivity = Class.forName(name);
Intent intent =
new
Intent(currentActivity,toActivity);
intent.putExtra(
"params"
, params);
currentActivity.startActivity(intent);
}
}
catch
(Exception e){
throw
new
JSApplicationIllegalArgumentException(
"不能打開Activity : "
+e.getMessage());
}
}
@ReactMethod
public
void
dataToJS(Callback successBack, Callback errorBack){
try
{
Activity currentActivity = getCurrentActivity();
String result = currentActivity.getIntent().getStringExtra(
"data"
);
if
(TextUtils.isEmpty(result)){
result =
"沒有數據"
;
}
successBack.invoke(result);
}
catch
(Exception e){
errorBack.invoke(e.getMessage());
}
}
//注意:startActivityFromJS、dataToJS方法添加RN注解(@ReactMethod),否則該方法將不被添加到RN中
}
二、
添加MyReactPackage類,實現ReactPackage接口里的方法暴露給RN調用,在重寫方法createNativeModules里注冊上一步添加的模塊:
public
class
MyReactPackage
implements
ReactPackage {
@Override
public
List<nativemodule> createNativeModules(ReactApplicationContext reactContext) {
return
Arrays.<nativemodule>asList(
new
MyIntentModule(reactContext));
}
@Override
public
List<viewmanager> createViewManagers(ReactApplicationContext reactContext) {
return
Collections.emptyList();
}
}
三、
接着在MainApplication中的getPackages方法中注冊到ReactPackage中:
@Override
protected
List<reactpackage> getPackages() {
return
Arrays.<reactpackage>asList(
new
MainReactPackage(),
new
MyReactPackage()
);
}
四、RN跳轉安卓
import
{
NativeModules,
TouchableNativeFeedback,
ToastAndroid
}from
'react-native'
_onPressButton() {
NativeModules
.IntentMoudle
.startActivityFromJS(
"com.myreactdemo.MyActivity"
,
null
);
}
render() {
return
(
<View>
<TouchableNativeFeedback onPress={
this
._onPressButton}>
<Text>跳轉到原生頁面</Text>
TouchableNativeFeedback>
<View>
);
}
五、安卓跳轉RN
1、顯式調用---直接調用Activity的Class類
例,Activity1調用Activity2
Intent intent = new Intent(currentActivity.this , MainActivity.class);
startActivity(intent);
例,Activity1調用Activity2
Intent intent = new Intent(currentActivity.this , MainActivity.class);
startActivity(intent);
2、隱式調用
Activity1隱式調用Activity2時需要在AndroidManifest.xml文件中配置Activity2的action和category,具體添加下面的代碼到Activity2的定義中
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="mycategory" />
</intent-filter>
接着同樣使用intent來啟動Activity,代碼如下:
Intent intent = new Intent("myaction2");
startActivity(intent);
這樣就可以啟動Activity2
注:在使用intent隱式調用Activity時會遇到多個Activity的intent-filter中的action和category相同時,這時android會先彈出一個選擇界面的窗口,顯式要啟動的Activity列表,根據用戶的選擇來啟動Activity,如Activity2和Activity3的action和category相同
<Activity android:name=".Activity2">
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="myCategory" />
</intent-filter>
</Activity>
<Activity android:name=".Activity3">
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="myCategory" />
</intent-filter>
</Activity>
啟動Activity代碼如下:
Intent intent = new("action2");
intent.addCategory("myCategory");
startActivity(intent);
這時就會彈出Acvity的選擇窗口,選擇啟動activity2還是activity3
Activity1隱式調用Activity2時需要在AndroidManifest.xml文件中配置Activity2的action和category,具體添加下面的代碼到Activity2的定義中
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="mycategory" />
</intent-filter>
接着同樣使用intent來啟動Activity,代碼如下:
Intent intent = new Intent("myaction2");
startActivity(intent);
這樣就可以啟動Activity2
注:在使用intent隱式調用Activity時會遇到多個Activity的intent-filter中的action和category相同時,這時android會先彈出一個選擇界面的窗口,顯式要啟動的Activity列表,根據用戶的選擇來啟動Activity,如Activity2和Activity3的action和category相同
<Activity android:name=".Activity2">
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="myCategory" />
</intent-filter>
</Activity>
<Activity android:name=".Activity3">
<intent-filter>
<action android:name="myaction2"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="myCategory" />
</intent-filter>
</Activity>
啟動Activity代碼如下:
Intent intent = new("action2");
intent.addCategory("myCategory");
startActivity(intent);
這時就會彈出Acvity的選擇窗口,選擇啟動activity2還是activity3