React Native Open Navigate to Another Activity on Button Click

This tutorial is the second part of Add New Activity in React Native Application tutorial. In this tutorial we would going to create an react native application with 2 different activities, First one is MainActivity and Second is SecondActivity. Now we would add a button in our MainActivity and on button onPress – onClick event we would call a function that will navigate us to SecondActivity.

Contents in this project Open Navigate to Another Activity on Button Click in React Native App :

1. Start a fresh React Native project. If you don’t know how then read my this tutorial.

2. Read our Previous tutorial to Add New Activity in Existing React Native Project.

3. Import react-navigation library in your project using using NPM server. So open your project’s folder in Command Prompt / Terminal and Type below command to install react-navigation library.

Screenshot of Command Prompt after installing the library :


4. Now open your project’s index.ios.js or index.android.js file and Import AppRegistry, StyleSheet, Text, View and Button component in your project.

5. Import StackNavigator module from react-navigation library in your project.

6. Create a new class named as MainActivity.

static navigationOptions : Used to set the Activity screen title shown inside the Title bar.

FunctionToOpenSecondActivity : This function allow us to navigate to the Second Activity.

7. Create another class named as SecondActivity .

8. Add StackNavigator method with your Application project name to call the activities like i did in below code. For example my project name is Project. This step is very important.

9. Add all below CSS classes.

10. Complete source code for App.js File :

Screenshot in Android Mobile Phone application :

Navigate

Screenshot in iOS iPhone mobile application :