简体   繁体   English

如何在反应原生应用程序中检测应用程序首次启动?

[英]How to detect application first launch in react native app?

In my scenario, I am having three different screens like Page1 , Page2 , Page3 .在我的场景中,我有三个不同的屏幕,例如Page1Page2Page3 Here, if the user last visited page 2 then next time if user open application, instead of showing page1 need to show page2.在这里,如果用户上次访问页面 2,那么下次如果用户打开应用程序,而不是显示 page1 需要显示 page2。 How to achieve this using a react-native application?如何使用react-native应用程序实现此目的?

I tried by using async storage but don't know how to manage multiple pages我尝试使用async存储,但不知道如何管理多个页面

AsyncStorage.getItem("alreadyLaunched").then(value => {
            if(value == null){
                 AsyncStorage.setItem('alreadyLaunched', true); // No need to wait for `setItem` to finish, although you might want to handle errors
                 this.setState({firstLaunch: true});
            }
            else{
                 this.setState({firstLaunch: false});
            }}) // Add some error handling, also you can simply do this.setState({fistLaunch: value == null})

App.js应用程序.js

import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';

import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
import ThirdPage from './pages/ThirdPage';

//import all the screens we are going to switch 
const App = createStackNavigator({
   //Constant which holds all the screens like index of any book 
   FirstPage: { screen: FirstPage, header: null},
   SecondPage: { screen: SecondPage,  headerLeft: null, headerBackTitle: null}, 
   ThirdPage: { screen: ThirdPage}, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);
export default createAppContainer(App);

You should use AsyncStorage to store the current screen, so any time you move from a screen to another you should call您应该使用 AsyncStorage 来存储当前屏幕,因此无论何时从一个屏幕移动到另一个屏幕,您都应该调用

AsyncStorage.setItem('screen', 'nameOfYourScreen')

and in App.js you should call在 App.js 中你应该调用

AsyncStorage.getItem('screen');

and affect it to initialRouteName并将其影响到 initialRouteName

PS: getItem() is asynchronous. PS:getItem() 是异步的。

Create another page say "Decider.js" and also import createSwitchNavigator from react-navigation.创建另一个页面说“Decider.js”,并从 react-navigation 导入 createSwitchNavigator。 and make it your default page every time app launches.并将其设置为每次应用启动时的默认页面。 and there we will check where was the user last time and navigate to that page.在那里我们将检查用户上次在哪里并导航到该页面。 Let achieve this as below:让我们实现如下:

App.js应用程序.js

import { createSwitchNavigator, createAppContainer } from 'react-navigation'; //new import createSwitchNavigator
import { createStackNavigator} from 'react-navigation-stack';

import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
import ThirdPage from './pages/ThirdPage';
import Deccider from './pages/Decider.js'; // New deciding screen

//import all the screens we are going to switch 
const App = createStackNavigator({
   //Constant which holds all the screens like index of any book 
   FirstPage: { screen: FirstPage, header: null},
   SecondPage: { screen: SecondPage,  headerLeft: null, headerBackTitle: null}, 
   ThirdPage: { screen: ThirdPage}, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);

export default createAppContainer(createSwitchNavigator(
    {
        AuthLoading: Decider,
        App: App,
    },
    {
        initialRouteName: 'AuthLoading',
    }
));

Now on every app launch first screen called will be Decider.js现在在每个应用程序启动时,调用的第一个屏幕将是 Decider.js

Decide.js决定.js

import React from 'react';
import {
    AsyncStorage,
    View,
} from 'react-native';

export default class AuthLoadingScreen extends React.Component {
    constructor() {
        super();
        this._bootstrapAsync();
    }

    _bootstrapAsync = async () => {
        var alreadyLaunchedPage = await
            AsyncStorage.getItem('alreadyLaunchedPage');
        if (alreadyLaunchedPage) {
            this.props.navigation.navigate(alreadyLaunchedPage);
        } else {
            this.props.navigation.navigate('App');
        }

    };

    render() {
        return (
            <View style={{ flex: 1 }}>
                {/* Any Loading or splash design */}
            </View>
        );
    }
}

**On every page's (FirstPage,SecondPage,ThirdPage) componentDidMount ** **在每个页面的(FirstPage,SecondPage,ThirdPage)componentDidMount **

...
componentDidMount(){
        AsyncStorage.setItem("alreadyLaunchedPage","FirstPage")//if FirstPage
     // AsyncStorage.setItem("alreadyLaunchedPage","SecondPage")//if SecondPage
     // AsyncStorage.setItem("alreadyLaunchedPage","ThirdPage")//if ThirdPage
    }
...

Hope it will solve your problem.希望它能解决你的问题。

NOTE : Here we have used createSwitchNavigator to prevent going back to Decider.js once you reach any of the FirstPage, SecondPage or ThirdPage.注意:这里我们使用createSwitchNavigator来防止在您到达 FirstPage、SecondPage 或 ThirdPage 中的任何一个时返回Decider.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM