Android深度链接与React Navigation 5



我正在尝试将React Navigation 5中的深度链接实现到我的React Native 0.61项目中,该项目是通过React Native init创建的。

当应用程序处于后台时,深度链接会起作用。在我的Android设备上,我可以点击https://myproject.com/content/5并且应用程序正确地导航到内容部分并显示id为5的内容。

然而,如果我关闭应用程序(或在不打开的情况下安装它(并点击同一链接,我会被带到主页,而不是导航到相应的内容页面。

我的AndroidManifest.xml中的活动:

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="http" />
<data android:scheme="https" />
<data android:host="myproject.com"/>
</intent-filter>
</activity>

肉&App.js的土豆:

const config = {
Content: {
path: "content/:id",
parse: {
id: Number
}
}
};

const App = () => {
const {state} = useContext(Context);
const ref = React.useRef();
const { getInitialState } = useLinking(ref, {
prefixes: ['http://myproject.com'],
config
});
const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState();
useEffect(() => {
Promise.race([
getInitialState(),
new Promise(resolve => setTimeout(resolve, 150)),
])
.catch(e => {
console.error(e);
})
.then(state => {
if (state !== undefined) {
setInitialState(state);
}
setIsReady(true);
});
}, [getInitialState]);
return (
<NavigationContainer initialState={initialState} ref={ref} >
{state.isLoading || !isReady ? 
(
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Loading" component={LoadingScreen} />
</Stack.Navigator>
)
:
<MainStack />
}
</NavigationContainer>
);
};

当安卓用户使用深度链接打开应用程序时,当该应用程序之前已被杀死时,我应该如何让该应用程序导航到内容页面?

我还想保持当前的深度链接行为,当应用程序在后台时,深度链接可以工作。

这里也解释了这个问题,但没有一个提议的解决方案对我有效

您的导航器具有动态路由切换功能,因此您需要先处理所有切换逻辑,然后在应用程序就绪后渲染导航器。当前,导航器会忽略initialState,动态切换会覆盖渲染的路线。

尝试将加载状态移动到导航器之外,或者确定应用程序已准备好在effect中显示导航器。

相关内容

最新更新