先来吐槽一下,React native学习的资料真的是很少啊,而且国内博客大部分都很老,还千篇一律,加了几个群,光有问的,没有回答的。简直了。。。
好了,现在来说一说怎么跳转到React native的某个页面:
React native现在提倡大家用React Navigation 来做导航器,如果不懂得,先去官网看看例子。这里页面跳转用StackNavigator,以下是示例代码:
const App = StackNavigator({
first: {
screen: First,
path:'app/first'
},
second: {
screen: Second,
path:'app/second'},
}, {
navigationOptions: {
header: null
},
});
大家可能知道screen,它就是要跳转的页面,但是对path可能不太清楚,下面是官网的介绍,当deep linking或者是一个web app的时候,这个就有用处了,所以今天app跳转原生就是由path来实现了。
在android/app/src/main/AndroidManifest.xml中的MainActivity
下添加
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="demo2"
android:host="demo2" />
</intent-filter>
然后通过在原生app处调用
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("demo2://demo2/app/first");
intent.setData(uri);
startActivity(intent);
就可以打开了,同样,如果有参数的话,首先path定义的时候要定义'app/first/:name',
然后调用的时候Uri uri = Uri.parse("demo2://demo2/app/first/rou");//first后边的就是参数
后记:
之前按照此方法是可行的,可是最近有很多人问我,怎么跳转不了了,我自己也试了一下,确实不可以了!需要在StackNavigator中增加一个URI的匹配项,代码如下:
import React, { Component } from 'react';
import {
StackNavigator,
} from 'react-navigation';
import Third from './page/third'
import First from './page/first'
import Second from './page/second'
const App = StackNavigator({
first: {
screen: First,
path:'app/first'
},
second: {
screen: Second,
path:'app/second'
},
third:{
screen: Third,
path:'app/third'
},
}, {
navigationOptions: {
header: null
},
});
const prefix = 'demo2://demo2/';
const MainApp = () => <App uriPrefix={prefix} />
export default MainApp ;
现在就可以跳转了!