Author: Han Ru
Company: Procedural Coffee (Beijing) Technology Co., LTD
Columnist for Hung Mun Bus
A, the same Page of AbilitySlice between the jump
1.1 the present
When the AbilitySlice that initiated the navigation and the AbilitySlice of the navigation target are on the same Page, the navigation can be achieved through the present() method.
@Override
protected void onStart(Intent intent) {
...
Button button = ...;
button.setClickedListener(listener -> present(new TargetSlice(), newIntent())); . }Copy the code
Here’s the present() method:
// Display another AbilitySlice that can use Intent objects to pass the desired information. public final void present(AbilitySlice targetSlice, Intent intent)Copy the code
Let’s add a button to the layout directory in ability_main.xml:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">
<Button
ohos:id="$+id:btn1"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Click the button to go to the first page."
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Next, we’ll create an XML file in the Layout directory to represent the second page to jump to, ability_second.xml,
<? xml version="1.0" encoding="utf-8"? > <DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:padding="10vp"
ohos:background_element="#2200AA00"
ohos:orientation="vertical">
<Text
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="Second page"
ohos:text_alignment="center"
ohos:text_size="20fp"
/>
<Button
ohos:id="$+id:btn2"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Click the button to go to the first page."
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
/>
</DependentLayout>
Copy the code
We create a new AbilitySlice file under the Slice package: SecondAbilitySlice. Java to load the ability_second.xml layout.
public class SecondAbilitySlice extends AbilitySlice{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_second); }}Copy the code
Then in MyAbilitySlice, get the Button component and add the click event.
package com.example.hanruabilityslicejump.slice;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 1.present-----------------------------------
// Get the button
Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
// Add a click event for the button
/** * present(AbilitySlice targetSlice, Intent Intent) * Set the component to be launched, and determine the actual location and target location. * /
btn1.setClickedListener(component -> present(new SecondAbilitySlice(),newIntent())); }}Copy the code
In the onStart() method of SecondAbilitySlice. Java, add a click event as well:
Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
btn2.setClickedListener(component -> present(new MainAbilitySlice(),new Intent()));
Copy the code
So we can click the button on the first page to go to the second page, and click the button on the second page to go to the first page.
Look at the effect:
1.2 presentForResult
The developer should use presentForResult() for navigation if he or she wants to be able to retrieve the result when the user returns from the navigation target, AbilitySlice. When the user returns from the navigation target AbilitySlice, the system will call onResult() to receive and process the returned result. The developer needs to override this method. The returned result is set by the navigation target AbilitySlice over its lifetime via setResult().
@Override
protected void onStart(Intent intent) {
...
Button button = ...;
button.setClickedListener(listener -> presentForResult(new TargetSlice(), new Intent(), 0)); . }@Override
protected void onResult(int requestCode, Intent resultIntent) {
if (requestCode == 0) {
// Process resultIntent here.}}Copy the code
Here’s the presentForResult() method:
/ / show another AbilitySlice and by calling the setResult (ohos. Aafwk. Content. Intent) return target AbilitySlice set of results.
/** * targetSlice: specifies the target AbilitySlice. * Intent, information carried by a jump, cannot be null. * requestCode, the custom requestCode, cannot be negative. * /
public final void presentForResult(AbilitySlice targetSlice, Intent intent, int requestCode)
Copy the code
Jump and send back. The procedure is as follows:
- 1. On page A, use presentForResult(AbilitySlice targetSlice, Intent Intent, int requestCode) to jump to the second page.
- 2. On page B, use setResult(Intent resultData). When the end of page B, it will return to page A.
- 3. On page A, onResult(int requestCode, Intent resultIntent) is executed.
- Verify that the requestCode is the requestCode that was sent
- Operate resultIntent to obtain data
Let’s add another button to our ability_main.xml:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">... <Button ohos:id="$+id:btn2"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Jump and return data"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Then create a new XML file, present_for_result.xml, in the Layout directory:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:padding="20vp"
ohos:orientation="vertical">
<Text
ohos:id="$+id:textmsg"
ohos:height="200vp"
ohos:width="match_parent"
ohos:background_element="#3300ff00"
ohos:text_size="25fp"
ohos:text_alignment="center"
/>
<Button
ohos:id="$+id:btnforresult"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Return the result to the previous page"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Then in MainAbilitySlice. Java, get this button to jump to the second page, and when the second page is destroyed, send back the data:
// presentForResult-----------------------------------
Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
/** * Select presentForResult(AbilitySlice targetSlice, Intent Intent, int requestCode) * 2. On the B page, use setResult(Intent resultData). When the B page ends, it returns to the A page. * 3. OnResult (int requestCode, Intent resultIntent) * verify that the requestCode is the requestCode that was sent * operate on the resultIntent */
btn2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
// 1. To jump to the second page and pass the value
Intent intent2 = new Intent();
intent2.setParam("msg"."You are the white rabbit?");
// Go to the details page and return the data
presentForResult(newPresentForResultAbilitySlice(),intent2,REQUESTCODE); }}});Copy the code
Then a new AbilitySlice files under the SRC: PresentForResultAbilitySlice. Java,
package com.example.hanruabilityslicejump.slice;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class PresentForResultAbilitySlice extends AbilitySlice{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_present_for_result);
// Receive the data in the Intent
String msg = intent.getStringParam("msg");
// Set the data to Text.
Text textMsg = (Text) findComponentById(ResourceTable.Id_textmsg);
textMsg.setText(msg);
// On the second page, click the button to return to the first page and return the data
Button btnForResult = (Button) findComponentById(ResourceTable.Id_btnforresult);
btnForResult.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
// Click the button, send back data, and destroy the current AbilitySlice, will return to A page.
// Send data back
Intent intent1 = new Intent();
intent1.setParam("backMsg"."I am a giraffe.");
setResult(intent1); // Return to page A,
System.out.println("B page. Send back data...");
terminate();// Destroy the current AbilitySlice
// present(new MainAbilitySlice(),intent1);}}); }}Copy the code
The idea here is to get the data from the previous page, click the button, and send the data back to the first page.
We then override the onResult() method on the first page to process the data passed back.
/ / by presentForResult () to jump to another page, and by calling the setResult (ohos. Aafwk. Content. Intent) return target AbilitySlice set of results.
@Override
protected void onResult(int requestCode, Intent resultIntent) {
System.out.println("requestCode-->"+requestCode);
System.out.println("-- >"+resultIntent);
switch (requestCode) {
case REQUESTCODE:
if(resultIntent ! =null){
String backMsg = resultIntent.getStringParam("backMsg");
System.out.println("backMsg-->"+backMsg);
new ToastDialog(getContext()).setText(backMsg+"").show();
}
break;
default:}}Copy the code
We run the program:
We can also look at the printed information:
Two, different pages of AbilitySlice between the jump
AbilitySlice is an internal unit of a Page and is exposed in the form of an Action. Therefore, you can configure an Intent Action to navigate to the target AbilitySlice. You cannot use present() or presentForResult() to navigate between different pages. You can use the startAbility() or startAbilityForResult() methods, and the callback to return the result is onAbilityResult(). Calling setResult() in Ability sets the result to be returned.
2.1 startAbility
Method 1: Launch the app using the full name of Ability.
The Ability to jump to is specified with withAbilityName() and withBundleName().
First, we’ll create a new one called Ability, otherability.java
package com.example.hanruabilityslicejump;
import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class OtherAbility extends Ability{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
// Set another Ability and load AbilitySlice.
super.setMainRoute(ThirdAbilitySlice.class.getName()); }}Copy the code
Select * from ‘ThirdAbilitySlice’ where ‘ThirdAbilitySlice’ = ‘ThirdAbilitySlice’ and ‘ThirdAbilitySlice’ = ‘ThirdAbilitySlice’ and ‘ThirdAbilitySlice.
package com.example.hanruabilityslicejump.slice;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class ThirdAbilitySlice extends AbilitySlice{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_otherability_third); }}Copy the code
The XML layout file loaded here is otherability_third, so we create a new layout file in the Layout directory: otherability_third.xml:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33aa00aa"
ohos:orientation="vertical">
<Text
ohos:height="match_parent"
ohos:width="match_parent"
ohos:text="I am the AbilitySlice of OtherAbility."
ohos:text_size="24fp"
ohos:text_alignment="center"
/>
</DirectionalLayout>
Copy the code
Finally, don’t forget to include OtherAbility in the config.json file: here we specify an action.
{
"skills": [{"actions": [
"action.other.show"]}],"orientation": "unspecified"."name": "com.example.hanruabilityslicejump.OtherAbility"."type": "page"."launchType": "standard"
}
Copy the code
If shown:
Then we add a third button in ability_main.xml:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">... <Button ohos:id="$+id:btn3"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Jump between AbilitySlice pages"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
In mainability.java, add a third button click event:
// 3.startAbility-----------------------------------
Button btn3 = (Button) findComponentById(ResourceTable.Id_btn3);
btn3.setClickedListener(component -> {
System.out.println("Btn3 click -- -- -- -- -- -");
// You can't use present() or presentForResult() to navigate between different pages
Intent intent3 = new Intent();
// specifies to jump to Ability with withAbilityName(), but withBundleName().
Operation operation = new Intent.OperationBuilder()
.withAbilityName(OtherAbility.class)
.withBundleName("com.example.hanruabilityslicejump")
.build();
intent3.setOperation(operation);
startAbility(intent3);
});
Copy the code
To run, click the third button:
Note here that you specify a jump to Ability with withAbilityName(), but you also need to use withBundleName().
Method 2: You can also specify the Action
In ability_main.xml, we add a fourth button:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">... <Button ohos:id="$+id:btn4"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Jump 2 between different Pages"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Then add the fourth button click event to mainability.java,
// 4.startAbility-----------------------------------
Button btn4 = (Button)findComponentById(ResourceTable.Id_btn4);
btn4.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
System.out.println("Click bTN4...");
Intent intent4 = new Intent();
// By specifying the Action.
// The setAction() method is obsolete.
//in.setAction("action.other.show"); // Specify the action value for AbilitySlice in another Page
Operation operation = new Intent.OperationBuilder()
.withAction("action.other.show") .build(); intent4.setOperation(operation); startAbility(intent4); }});Copy the code
Run:
If a want to jump to a different Page in another AbilitySlice, you can operate as follows.
First create an XML layout file, otherability_four.xml:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33aaaa00"
ohos:orientation="vertical">
<Text
ohos:height="match_parent"
ohos:width="match_parent"
ohos:text="I am another AbilitySlice of OtherAbility."
ohos:text_size="24fp"
ohos:multiple_lines="true"
ohos:text_alignment="center"
/>
</DirectionalLayout>
Copy the code
Then create a new AbilitySlice: FourAbilitySlice. Java and specify that this XML file should be loaded:
package com.example.hanruabilityslicejump.slice;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class FourAbilitySlice extends AbilitySlice {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_otherability_four); }}Copy the code
Configure routing in Ability to enable navigation to the corresponding Abilitysice using this action.
package com.example.hanruabilityslicejump;
import com.example.hanruabilityslicejump.slice.FourAbilitySlice;
import com.example.hanruabilityslicejump.slice.StartAbilityForResultAbilitySlice;
import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class OtherAbility extends Ability{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
// Set the main route to AbilitySlice by default.
super.setMainRoute(ThirdAbilitySlice.class.getName());
// set the action route
super.addActionRoute("action.other.four", FourAbilitySlice.class.getName()); }}Copy the code
Here the action: “action.other.four”, also needs to be configured in config.json:
"skills": [
{
"actions": [
"action.other.show",
"action.other.four"
]
}
],
Copy the code
Then add another button in ability_main.xml: the fifth button
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">... <Button ohos:id="$+id:btn5"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Another AbilitySlice between different pages"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
If we want to open FourAbilitySlice in OtherAbility by clicking the fifth button:
// 5.startAbility-----------------------------------
Button btn5 = (Button)findComponentById(ResourceTable.Id_btn5);
btn5.setClickedListener(component->{
System.out.println("Click bTN5...");
Intent intent5 = new Intent();
// By specifying the Action.
Operation operation = new Intent.OperationBuilder()
.withAction("action.other.four")
.build();
intent5.setOperation(operation);
startAbility(intent5);
});
Copy the code
Operation effect:
Action here, can also use some system, such as open dial and so on. We can go into more detail in the Intent section.
2.2 startAbilityForResult
Here’s the train of thought:
1. In the first AbilitySlice of Ability, construct the Intent and Operation object containing the Action, and invoke the startAbilityForResult() method to initiate the request.
2, jump to another AbilitySlice specified by startAbilityForResult().
Process the request in another Ability, and call the setResult() method to store the returned result.
4. Return to the first Ability and rewrite onAbilityResult() to process the returned result.
Create an XML file start_ability_for_result. XML in the layout directory and use it as the layout page to jump to.
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:padding="20vp"
ohos:orientation="vertical">
<Text
ohos:height="match_content"
ohos:width="match_parent"
ohos:text_size="25fp"
ohos:text="Different PageAbility"
ohos:text_alignment="center"
ohos:bottom_margin="30vp"
/>
<Text
ohos:id="$+id:textmsg"
ohos:height="200vp"
ohos:width="match_parent"
ohos:background_element="#3300ffff"
ohos:text_size="25fp"
ohos:text_alignment="center"
/>
<Button
ohos:id="$+id:btnforresult2"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Return the result to the previous page"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Then create a new AbilitySlice under slice, StartAbilityForResultAbilitySlice. Java, used to want to jump to the interface, first to load a layout, XML is just created above.
public class StartAbilityForResultAbilitySlice extends AbilitySlice {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_start_ability_for_result);
}
Copy the code
We need to set the action in OtherAbility:
public class OtherAbility extends Ability{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
// Set the main route to AbilitySlice by default.
super.setMainRoute(ThirdAbilitySlice.class.getName());
// set the action route
super.addActionRoute("action.other.four", FourAbilitySlice.class.getName());
super.addActionRoute("action.other.result", StartAbilityForResultAbilitySlice.class.getName());
}
Copy the code
And register the action in config.json:
"skills": [{"actions": [
"action.other.show"."action.other.four"."action.other.result"]}]Copy the code
Next, in ability_main.xml, add another button:
<? xml version="1.0" encoding="utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#33AA0000"
ohos:orientation="vertical">... <Button ohos:id="$+id:btn6"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Jump to AbilitySlice and return data between different pages"
ohos:center_in_parent="true"
ohos:text_size="20fp"
ohos:multiple_lines="true"
ohos:layout_alignment="horizontal_center"
ohos:background_element="#EEEEEE"
ohos:padding="10vp"
ohos:top_margin="20vp"
/>
</DirectionalLayout>
Copy the code
Add the click event for button 6 to MainAbility, specifying the action:
// 6.startAbilityForResult-----------------------------------
Button btn6 = (Button)findComponentById(ResourceTable.Id_btn6);
btn6.setClickedListener(component-> {
System.out.println("Click bTN6...");
Intent intent6 = new Intent();
intent6.setParam("message"."Facing the sea, with spring blossoms.");
Operation operation = new Intent.OperationBuilder()
.withAction("action.other.result") / / specified Action
.build();
intent6.setOperation(operation);
startAbilityForResult(intent6,REQUESTCODEFORRESULT);
});
Copy the code
We need the StartAbilityForResultAbilitySlice. In Java, processed the data you send:
package com.example.hanruabilityslicejump.slice;
import com.example.hanruabilityslicejump.MainAbility;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class StartAbilityForResultAbilitySlice extends AbilitySlice {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_start_ability_for_result);
// Receive the data in the Intent
String msg = intent.getStringParam("message");
// Set the data to Text.
Text textMsg = (Text) findComponentById(ResourceTable.Id_textmsg);
textMsg.setText(msg);
// On the second page, click the button to return to the first page and return the data
Button btnForResult2 = (Button) findComponentById(ResourceTable.Id_btnforresult2);
btnForResult2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
// Destroy current AbilitySlice, return to A page.
terminate();// Destroy the current AbilitySlice}}); }}Copy the code
Here we display the data from the previous page to the Text. In the click event of the button, we simply call Terminate () and destroy the current AbilitySlice, which will return to the previous page.
MainAbilitySlice (); onActive(); onActive();
package com.example.hanruabilityslicejump;
import com.example.hanruabilityslicejump.slice.FourAbilitySlice;
import com.example.hanruabilityslicejump.slice.StartAbilityForResultAbilitySlice;
import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class OtherAbility extends Ability{
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
// Set the main route to AbilitySlice by default.
super.setMainRoute(ThirdAbilitySlice.class.getName());
// set the action route
super.addActionRoute("action.other.four", FourAbilitySlice.class.getName());
super.addActionRoute("action.other.result", StartAbilityForResultAbilitySlice.class.getName());
}
@Override
protected void onActive(a) {
super.onActive();
System.out.println("======OtherAbility=======onActive()");
Intent intent1 = new Intent();
intent1.setParam("backMessage"."The sea of stars");
setResult(0,intent1); //0 is the returned resultCode after the current Ability is destroyed.
System.out.println("B page. Send back data..."); }}Copy the code
After clicking the button to jump over, let’s look at the printed result:
Then we process the result in onAbilityResult() :
// Process the result sent back by startAbilityForResult()
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
System.out.println("requestCode:" + requestCode + ", resultCode:-->" + resultCode);
if (requestCode == REQUESTCODEFORRESULT && resultCode == 0) {
if(resultData ! =null) {
String backMessage = resultData.getStringParam("backMessage");
System.out.println("backMessage-->" + backMessage);
new ToastDialog(getContext()).setText(backMessage + "").show();
}else{
new ToastDialog(getContext()).setText("No data returned...").show(); }}}Copy the code
Ok, let’s run it through:
On the second page, we can click the button or directly click the back button to go back to the first page and get the data sent back.