# arouter-api-onActivityResult **Repository Path**: krri/arouter-api-onActivityResult ## Basic Information - **Project Name**: arouter-api-onActivityResult - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 19 - **Created**: 2024-10-12 - **Last Updated**: 2025-02-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Arouter ## Introduction Arouter is a lightweight and efficient page routing tool for OpenHarmony. It provides the following functions: - Route switching between pages - Redirection and callback with parameters - Redirection interceptor configuration - Configuration of redirection preprocessing - Supports cross-module routing ## Preview ![img.gif](img_EN.gif) ## How to Install ```` ohpm install @ohos/arouteronactivityresult ```` For details about the OpenHarmony ohpm environment configuration, see [OpenHarmony HAR](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage_en.md). ## How to Use ### Page Routing #### 1. Routing without parameter transferring Use **Arouter.getInstance()** to create a route object. Call **build('')** (chain call method) to configure the page to redirect to, and **navigation()** to redirect to the target page. ``` import {Arouter} from "@ohos/arouteronactivityresult"; Arouter.getInstance() .build("--/--") // URL of the page to redirect to. .navigation() ``` #### 2. Routing with parameter transferring If no parameter is transferred for redirection, **withParams()** is used to set parameters before redirection. ``` import {Arouter} from "@ohos/arouteronactivityresult"; Arouter.getInstance() .build("--/--") // URL of the page to redirect to. .withParams({index:"--"}) .navigation() ``` #### 3. Route callback Route callback needs to be implemented together with the **NavigationCallback** API. The **NavigationCallback** API is implemented on the page before routing. ``` import {NavigationCallback} from '@ohos/arouteronactivityresult' var callback:NavigationCallback = { onInterrupt(postcard){}, onArrival(postcard){}, onActivityResult(data){} } ``` Transfer callback to **.navigationWithCallback()** for redirection. ``` import {Arouter} from "@ohos/arouteronactivityresult"; Arouter.getInstance() .build("--")// URL of the page to redirect to. .navigationWithCallback(callback) ``` In the lifecycle of **onPageShow()** of the target page, call the **getPostcard()** API to obtain the specified postcard. ``` import router from '@ohos.router'; if (postcard == null) { postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name); } ``` Use **postcard.getNavigationCallback()** to invoke the corresponding callback method. This operation calls back the method implemented by the source page. ``` postcard.getNavigationCallback().onActivityResult(params) ``` ### Route Interception #### 1. Interceptor configuration Implement page interception in the **process()** API of the interceptor. Call **interceptorCallback.onInterrupt()** to interrupt the redirection and **interceptorCallback.onContinue()** to resume the redirection. ``` import {Postcard,IInterceptor,InterceptorCallback} from '@ohos/arouteronactivityresult'; var iInterceptor:IInterceptor= { process(postcard:Postcard, interceptorCallback:InterceptorCallback) { // Select the page to be intercepted. If the target page URL exists during redirection, a message is displayed, indicating that the target page redirection is intercepted. If the target page URL does not exist, the redirection goes on without any interception. if (Postcard.getUri() == 'pages/transit') { // Select a dialog box. AlertDialog.show( { message 'The redirection is intercepted. Touch to continue.', primaryButton: { value: 'Cancel', action: () => { // Interrupt the redirection. interceptorCallback.onInterrupt(postcard) } }, secondaryButton: { value 'Continue', action: () => { // Resume the redirection. interceptorCallback.onContinue(postcard); } }, } ) } else { // Resume the redirection. interceptorCallback.onContinue(postcard); } } } ``` #### 2. Registering an interceptor ``` import {registerInterceptor} from '@ohos/arouteronactivityresult'; registerInterceptor(iInterceptor); ``` #### 3. Unregistering the interceptor ``` import {unregisterInterceptor} from '@ohos/arouteronactivityresult'; unregisterInterceptor() ``` #### 4. Setting a green channel Call the **.setGreenChannel()** API to skip interception before redirection. Value **true** indicates to skip interception. ``` Arouter.getInstance() .build("--/--")// URL of the page to redirect to. .setGreenChannel(true) .navigation() ``` #### 5. Configuring whether to pretreat redirection Call **onPretreatment** in the **PretreatmentService** API to implement pretreatment. A Boolean value is returned. The value **true** indicates that redirection continues, and **false** indicates no redirection. ``` import {PretreatmentService} from '@ohos/arouteronactivityresult'; var pretreatmentService:PretreatmentService = { onPretreatment(postcard:Postcard):boolean{ return true } } ``` Before redirection, call **.setPretreatmentService()** and transfer **pretreatmentService** to implement pretreatment. ``` Arouter.getInstance() .build(this.router) .setPretreatmentService(pretreatmentService) .navigationWithCallback(callback) ``` #### 6.Cross-module route jump api10+ 1. The dependencies need to be configured in the oh-package.json5 file of the current application package. For example:: ``` "dependencies": { "@ohos/libraryOne": "file:../libraryOne", ... } ``` 2. In the destination page of the jump, name the @Entry custom component EntryOptions ``` @Entry({ routeName: 'myPageOne' }) @Component struct Index { ... } ``` 3. Introduce the target page inside the page that triggers the jump, using Arouter ``` import '@ohos/library/src/main/ets/pages/Index'; ... Arouter.getInstance() .build("myPageOne",true) .setGreenChannel(true) .navigation() ... ``` ## Available APIs ### Arouter |API|Parameter|Description| |:----|:----|:----| |build|(string,boolean)|Configures a page redirection path(required), whether to jump across pages (non-required default is false)| |withParams|{ }|Sets the parameter to be transferred to another page.| |navigation| |Normal redirection.| |navigationWithCallback|NavigationCallback|Redirection with callback.| |setGreenChannel|boolean|Sets whether to configure a green channel.| |registerInterceptor|iInterceptor|Registers an interceptor.| |unregisterInterceptor| |Unregisters the interceptor.| |getNavigationCallback| |Obtains the status callback method.| |setUri|string|Sets the page redirection path.| |getUri| |Obtains the URL of the page to be redirected to.| |getParams| |Obtains the parameters transferred during redirection.| |getTag| |Obtains a tag.| |setTag|{ }|Sets a tag.| |withFlags|boolean|Sets flags.| |getFlags|boolean|Obtains flags.| |toString| |Exports a string.| |setPretreatmentService|PretreatmentService|Pretreatment.| |getPostcard|Postcard|Gets the specified postcard.| ### Callback APIs |API|Parameter|Description| |:---:|:---:|:---:| |NavigationCallback.onArrival|Postcard|Navigates to the callback position.| |NavigationCallback.onInterrupt|Postcard|Interrupts the callback.| |NavigationCallback.onActivityResult|any|Callback result.| |IInterceptor.process|Postcard,InterceptorCallback|Interception process.| |InterceptorCallback.onContinue|Postcard|Continues the interceptor callback.| |InterceptorCallback.onInterrupt|Postcard|Pauses the interceptor callback.| |PretreatmentService.onPretreatment|Postcard|Implements pretreatment.| ## Constraints This project has been verified in the following version: DevEco Studio: NEXT Release(5.0.3.900), SDK: API12 (5.0.0.71) DevEco Studio 5.0.1 Release(5.0.5.306), SDK: API10 (4.0.0) ## Directory Structure ```` |---- arouteronactivityresult | |---- src/main/ets/components/arouter # Implementation logic of route redirection | |---- index.ets # External APIs ```` ## How to Contribute If you find any problem during the use, submit an [Issue](https://gitee.com/openharmony-tpc/arouter-api-onActivityResult/issues) or a [PR](https://gitee.com/openharmony-tpc/arouter-api-onActivityResult/pulls) to us. ## License This project is licensed under [Apache License 2.0](https://gitee.com/openharmony-tpc/arouter-api-onActivityResult/blob/master/LICENSE).