{"id":15973412,"url":"https://github.com/ArTemmey/react-native-android-navigation","last_synced_at":"2025-08-05T01:30:46.655Z","repository":{"id":57335277,"uuid":"126463903","full_name":"ArTemmey/react-native-android-navigation","owner":"ArTemmey","description":"True Android native navigation for React Native apps","archived":false,"fork":false,"pushed_at":"2018-03-28T13:55:08.000Z","size":75,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-08T05:06:57.027Z","etag":null,"topics":["android","androidnavigator-methods","intent-methods","navigation-methods","react-native","react-native-android","react-native-app","react-native-navigation","react-native-navigator","reactnative","reactnativeandroid"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ArTemmey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-23T09:38:01.000Z","updated_at":"2022-02-10T09:21:37.000Z","dependencies_parsed_at":"2022-08-29T19:41:30.374Z","dependency_job_id":null,"html_url":"https://github.com/ArTemmey/react-native-android-navigation","commit_stats":null,"previous_names":["artemiydmtrvch/react-native-android-navigation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArTemmey%2Freact-native-android-navigation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArTemmey%2Freact-native-android-navigation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArTemmey%2Freact-native-android-navigation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArTemmey%2Freact-native-android-navigation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ArTemmey","download_url":"https://codeload.github.com/ArTemmey/react-native-android-navigation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228689672,"owners_count":17957605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","androidnavigator-methods","intent-methods","navigation-methods","react-native","react-native-android","react-native-app","react-native-navigation","react-native-navigator","reactnative","reactnativeandroid"],"created_at":"2024-10-07T21:04:49.583Z","updated_at":"2024-12-08T05:07:01.365Z","avatar_url":"https://github.com/ArTemmey.png","language":"JavaScript","readme":"# React Native Android Navigation [![npm version](https://badge.fury.io/js/react-native-android-navigation.svg)](https://badge.fury.io/js/react-native-android-navigation)\n\n*Let your application feel at ease.*\n\nReact Native Android Navigation is the new way to integrate JavaScript and Java/Kotlin inside React Native. \nWith it's help you can easily handle native window or background task in your project and also interact with other applications.\n\n* [Getting started](#getting-started)\n* [Installation](#installation)\n  * [Adding the dependency](#adding-the-dependency)\n  * [Linking](#linking)\n    * [Automatic](#automatic)\n    * [Manual](#manual)\n* [Usage](#usage)\n  * [Intent methods](#intent-methods)\n    * [Initialization](#initialization)\n    * [Extras](#extras)\n      * [Putting](#putting)\n      * [Getting](#getting)\n      * [Other extras methods](#other-extras-methods)\n    * [Categories and flags](#categories-and-flags)\n  * [AndroidNavigator methods](#androidnavigator-methods)\n    * [Activity processing](#activity-processing)\n      * [Starting Activity](#starting-activity)\n      * [Retrieving the result](#retrieving-the-result)\n      * [Handling the back press](#handling-the-back-press)\n      * [Other Activity methods](#other-activity-methods)\n    * [Service processing](#activity-processing)\n      * [Starting Service](#starting-service)\n      * [Adding custom tasks](#adding-custom-tasks)\n    * [Catching errors](#catching-errors)\n\t\n## Getting started\n\nNative Android navigation is made up of two main parts:\n\n1. Navigation methods, available inside the native window — `Activity` — and inside the background task worker — `Service`;\n \n2. Object with the navigation data — `Intent`, which is passed into the navigation methods to direct the router. It's also transferred to the destination.\n\nBased on this, React Native Android Navigation has two classes for usage:\n  \n1. `AndroidNavigator` — class that wraps all the navigation methods as static properties;\n \n2. `Intent` — native Intent wrapper.\n\nSimple example:\n```javascript\nimport {\n    AndroidNavigator,\n    Intent\n} from 'react-native-android-navigation';\n\nconst intent = new Intent();\nintent.setClassName('your.package.name.YourActivity');\nAndroidNavigator.startActivity(intent);\n```\n*\"your.package.name\" is the path from `\u003cYourProject\u003e/android/app/src/main/java` to the directory where your Activity class is located.*\n\n## Installation\n\n### Adding the dependency\n\n  ```bash\n  yarn add react-native-android-navigation\n  ```\n\n  or\n\n  ```bash\n  npm install --save react-native-android-navigation\n  ```\n\n### Linking\n\n#### Automatic\n\n  ```bash\n  react-native link react-native-android-navigation\n  ```\n\n#### Manual\n\n1. Open `\u003cYourProject\u003e/android/settings.gradle` and add the following:\n  ```\n    include ':react-native-android-navigation'\n    project(':react-native-android-navigation').projectDir = new File(rootProject.projectDir,   '../node_modules/react-native-android-navigation/android')\n  ```\n2. Open `\u003cYourProject\u003e/android/app/build.gradle` and add inside the dependencies block:\n  ```\n    compile project(':react-native-android-navigation')\n  ```\n3. Open `\u003cYourProject\u003e/android/app/src/main/java/package.name/MainApplication.java`:\n  - Add `import com.navigation.NavigationPackage;`\n  - Add `new NavigationPackage()` to the list returned by `getPackages()` method\n\n## Usage\n\n### Intent methods\n\n#### Initialization\n\nIntent has four methods for initialization. Call them separately or in combination with each other:\n\n1. `setClassName(className: string): Intent`\n  \n\tUse it separately if the target Activity is inside your application. Combine with `setPackageName` if the target Activity is inside another application.\n  \n2. `setPackageName(packageName: string): Intent`\n  \n\tUse it separately to get the launcher Intent for the target package. Combine with `setClassName` if you need to start a certain Activity from the target package.\n\n3. `setAction(action: string): Intent`\n\n\tRepresents Android Intent method `setAction`. Note that JS Intent class has all the default Android Intent actions as static properties, so use them them the same way as in Android: `intent.setAction(Intent.ACTION_MAIN)`.\n\n4. `setCustomServiceEventName(eventName: string): Intent`\n\n\tUse it separately to start [your custom Service](#adding-custom-tasks). Combine with `setPackageName` if the target Service is inside another application.\n\n#### Extras\n\nIntent allows you to pass extra data of any type.\n\n##### Putting\n\nPutting extras is done in the same way as in Android, with the following methods:\n  - `putExtra(key: string, extra: any): Intent`\n  - `putExtras(extras: Object): Intent`\n\nExample:\n```javascript\nintent\n    .putExtra(\"key1\", \"Hello world!\")\n    .putExtras(\t//New data will be added to the existing\n        {\n            key2: 25.001,\n            key3: [\"pass array\", true, 12345],\n            key4: {key1: \"object as well\", key2: 69.999}\n        }\n    );\n```\n\n##### Getting\n\nOn Java side, extras can be obtained as usual.\nFor primitives and strings use `getBooleanExtra`, `getIntExtra`, `getDoubleExtra`, `getStringExtra`.\nFor arrays and objects use `getSerializableExtra` and cast the result to `List` or `Map` respectively. Then if needed cast each their child to its class.\n\nExample:\n```java\nString helloWorld = intent.getStringExtra(\"key1\"));\nList list = (List) intent.getSerializableExtra(\"key3\");\nboolean b = (Boolean) list.get(1);\nMap map = (Map) intent.getSerializableExtra(\"key4\");\ndouble d = (Double) map.get(\"key2\");\n```\n\nOn JS side, just use the following methods:\n  - `getExtra(key: string): any`\n  - `getExtras(): Object`\n\n##### Other extras methods \n\n  - `removeExtra(key: string): void`\n  - `replaceExtras(extras: Object): Intent`\n  - `hasExtra(key: string): boolean`\n\n#### Categories and flags\n\nTo deal with categories and flags the same methods are used as in Android:\n  - `addCategory(category: string): Intent`\n  - `removeCategory(category: string): void`\n  - `getCategories(): string[]`\n  - `hasCategory(category: string): boolean`\n  - `addFlags(flags: number): Intent`\n  - `setFlags(flags: number): Intent`\n  - `getFlags(): number[]`\n  - `removeFlags(flags: number): void`\n  \nLike the actions, all the default Android Intent categories and flags are declared in JS Intent class as static properties, so use them the same way as in Android.\n\nExample:\n```javascript\nintent.addCategory(Intent.CATEGORY_DEFAULT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n```\n\n### AndroidNavigator methods\n\n#### Activity processing\n\n##### Starting Activity\n\nTo start new Activity, use the same methods as in Android:\n \n  - `static startActivity(intent: Intent): Promise\u003cvoid\u003e`\n  \n  \t[May trow error](#catching-errors)\n  \t\n  - `static startActivityForResult(intent: Intent, requestCode: number): Promise\u003cvoid\u003e`\n  \n  \tUse it to get the data you need from the target Activity after it's finished. `requestCode` can be any integer.\n  \t\n  \t[May trow error](#catching-errors)\n\n##### Retrieving the result \n\nTo get the result, add event listener for the corresponding `ActivityEventType`:\n\n`export type ActivityResultListener = (requestCode: number, resultCode: number, data: Intent | null) =\u003e void`\n\nExample:\n```javascript\nconst myOnResult = (requestCode, resultCode, data) =\u003e {\n    console.log(data.getExtras().key1);\n};\nAndroidNavigator.addEventListener(ActivityEventType.ACTIVITY_RESULT, myOnResult); //myOnResult will be called each time Activity result is received\n```\n\n##### Handling the back press\n\nReact Native Android Navigation allows you to pass any data from native side on back press event.\n\nOn Java side, open file `\u003cYourProject\u003e/android/app/src/main/java/your.package.name/MainActivity.java`:\n\n  - Add `import com.navigation.NavigationModule;`\n  - Add inside the Activity class:\n  ```java\n  @Override\n  public void onBackPressed() {\n      NavigationModule.onBackPressed(\"My data\", 123); //pass rest parameters\n  }\n  ```\n\nOn JS side, add event listener for the corresponding `ActivityEventType`:\n\n`export type BackPressListener = (...data: any) =\u003e void`\n\nExample:\n```javascript\nconst myOnBackPressed = (myData, oneTwoThree) =\u003e {\n    console.log(myData);\n};\nAndroidNavigator.addEventListener(ActivityEventType.BACK_PRESSED, myOnBackPressed); //myOnBackPressed will be called each time back button is pressed\n```\n\n##### Other Activity methods\n\n  - `static currentActivityIsRunning(): Promise\u003cboolean\u003e`\n  \n    Checks if your app has any running Activity.\n    \n    [May trow error](#catching-errors)\n    \n  - `static getIntent(): Promise\u003cIntent\u003e`\n  \n    Gets the Intent with which Activity was started.\n    \n    [May trow error](#catching-errors)\n    \n  - `setResult(resultCode: number, data?: Intent): Promise\u003cvoid\u003e`\n  \n    Use it if Activity was started for result. \n    Possible values of `resultCode` are included in AndroidNavigator as static properties: `RESULT_CANCELED = 0`, `RESULT_FIRST_USER = 1`, `RESULT_OK = -1`\n    \n    [May trow error](#catching-errors)\n    \n  - `finish(): Promise\u003cvoid\u003e`\n  \n    Closes current Activity.\n    \n    [May trow error](#catching-errors)\n\n#### Service processing\n\n##### Starting Service\n\nTo start Service, use the same method as in Android:\n\n  - `startService(intent: Intent): Promise\u003cvoid\u003e`\n  \n  \t [May trow error](#catching-errors)\n\n##### Adding custom tasks\n\nTo add your custom task, add it's event listener:\n\n`export type CustomServiceEventListener = (extras: Object) =\u003e void`\n\nand then start it with `startService` method, passing Intent, initialized with `setCustomServiceEventName`.\n\nExample:\n```javascript\nconst myServiceListener = (eventExtras) =\u003e {\n    console.log(eventExtras.myData);\n};\nAndroidNavigator.addEventListener(\"myService\", myServiceListener);\nconst intent = new Intent();\nintent.setCustomServiceEventName(\"myService\");\nintent.putExtra(\"myData\", \"Hello world!\");\nAndroidNavigator.startService(intent);\n```\n\n#### Catching errors\n\nThe following errors can be thrown while using AndroidNavigator methods:\n\n```javascript\nexport class NavigationError extends Error {\n    constructor(message: NavigationErrorMessage) {}\n}\n```\n```typescript\nexport enum NavigationErrorMessage {\n    TARGET_CLASS_NOT_FOUND = \"TARGET_CLASS_NOT_FOUND\",\n    TARGET_PACKAGE_NOT_FOUND = \"TARGET_PACKAGE_NOT_FOUND\",\n    TARGET_CLASS_NOT_EXPORTED = \"TARGET_CLASS_NOT_EXPORTED\"\n}\n```\n```javascript\nexport class NoActivityError extends Error {\n    constructor(message: string) {}\n}\n```\n\nTo catch them, add `catch` block on any Promise returned by AndroidNavigator method.\n\nExample:\n```javascript\nAndroidNavigator\n    .startActivity(intent)\n    .catch(\n        (e) =\u003e {\n            console.log(e.message);\n        }\n    );\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FArTemmey%2Freact-native-android-navigation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FArTemmey%2Freact-native-android-navigation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FArTemmey%2Freact-native-android-navigation/lists"}