找回密码
 立即注册
首页 业界区 业界 ArkUI-X在Android上使用Fragment开发指南

ArkUI-X在Android上使用Fragment开发指南

度阡舅 2025-6-12 21:04:55
本文介绍将ArkUI框架的UIAbility跨平台部署至Android平台Fragment的使用说明,实现Android原生Fragment和ArkUI跨平台Fragment的混合开发,方便开发者灵活部署跨平台界面。
Android工程配置

Android工程的PackageName需要与OpenHarmony工程的BundleName一致;

请在Android应用的gradle.properties文件,使能AndroidX:
  1. android.useAndroidX=true
复制代码
请在Android应用的build.gradle文件增加AndroidX Fragment库的依赖项:
  1. dependencies {
  2.     implementation  'androidx.appcompat:appcompat:1.4.1'
  3. }
复制代码
ArkUI-X和Android平台集成所用关键类

应用工程Android逻辑部分的StageApplication

应用需要继承arkui_android_adapter.jar包所提供的StageApplication。StageApplication用于初始化资源路径以及加载配置信息,例如:
  1. package com.example.myapplication;
  2. import ohos.stage.ability.adapter.StageApplication;
  3. public class MyApplication extends StageApplication {
  4.        
  5. }
复制代码
应用工程Android逻辑部分Fragment的宿主Activity

原生Activity需要继承androidx.fragment.app.FragmentActivity,绑定StageFragment示例如下:
  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.FragmentActivity;
  4. import androidx.fragment.app.FragmentManager;
  5. import ohos.stage.ability.adapter.StageFragment;
  6. public class MainActivity extends FragmentActivity {
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9. <?xml version="1.0" encoding="utf-8"?>
  10. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  11.     android:orientation="vertical"
  12.     android:layout_width="match_parent"
  13.     android:layout_height="match_parent">
  14.     <LinearLayout
  15.         android:id="@+id/frag"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="match_parent"
  18.         android:orientation="horizontal">
  19.     </LinearLayout>
  20. </LinearLayout>super.onCreate(savedInstanceState);
  21. <?xml version="1.0" encoding="utf-8"?>
  22. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  23.     android:orientation="vertical"
  24.     android:layout_width="match_parent"
  25.     android:layout_height="match_parent">
  26.     <LinearLayout
  27.         android:id="@+id/frag"
  28.         android:layout_width="match_parent"
  29.         android:layout_height="match_parent"
  30.         android:orientation="horizontal">
  31.     </LinearLayout>
  32. </LinearLayout>setContentView(R.layout.activity_main);
  33. <?xml version="1.0" encoding="utf-8"?>
  34. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  35.     android:orientation="vertical"
  36.     android:layout_width="match_parent"
  37.     android:layout_height="match_parent">
  38.     <LinearLayout
  39.         android:id="@+id/frag"
  40.         android:layout_width="match_parent"
  41.         android:layout_height="match_parent"
  42.         android:orientation="horizontal">
  43.     </LinearLayout>
  44. </LinearLayout>StageFragment fragment = new HiFragment();
  45. <?xml version="1.0" encoding="utf-8"?>
  46. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.     android:orientation="vertical"
  48.     android:layout_width="match_parent"
  49.     android:layout_height="match_parent">
  50.     <LinearLayout
  51.         android:id="@+id/frag"
  52.         android:layout_width="match_parent"
  53.         android:layout_height="match_parent"
  54.         android:orientation="horizontal">
  55.     </LinearLayout>
  56. </LinearLayout>FragmentManager manager = getSupportFragmentManager();
  57. <?xml version="1.0" encoding="utf-8"?>
  58. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  59.     android:orientation="vertical"
  60.     android:layout_width="match_parent"
  61.     android:layout_height="match_parent">
  62.     <LinearLayout
  63.         android:id="@+id/frag"
  64.         android:layout_width="match_parent"
  65.         android:layout_height="match_parent"
  66.         android:orientation="horizontal">
  67.     </LinearLayout>
  68. </LinearLayout>manager.beginTransaction().add(R.id.frag,fragment).commit();
  69.     }
  70. }
复制代码
其中activity_main.xml文件示例如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <LinearLayout
  7.         android:id="@+id/frag"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:orientation="horizontal">
  11.     </LinearLayout>
  12. </LinearLayout>
复制代码
如果当前StageFragment对应的UIAbility涉及页面跳转,Activity需要重写onBackPressed方法,以便在手机back键点击或手势侧滑时逐级返回页面,否则会退出跨平台承载的Activity页面。
  1. @Overridepublic void onBackPressed() {    if(fragment.onBackPressed()) {<?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <LinearLayout
  7.         android:id="@+id/frag"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:orientation="horizontal">
  11.     </LinearLayout>
  12. </LinearLayout>super.onBackPressed();    }}
复制代码
参数传递

StageFragment支持传递参数,参数的传递需要通过setArguments()进行设置,规则如下:

key值为params
value为json格式
  1. {    "params":[<?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <LinearLayout
  7.         android:id="@+id/frag"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:orientation="horizontal">
  11.     </LinearLayout>
  12. </LinearLayout>{<?xml version="1.0" encoding="utf-8"?>
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  14.     android:orientation="vertical"
  15.     android:layout_width="match_parent"
  16.     android:layout_height="match_parent">
  17.     <LinearLayout
  18.         android:id="@+id/frag"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="match_parent"
  21.         android:orientation="horizontal">
  22.     </LinearLayout>
  23. </LinearLayout>    "key":键,<?xml version="1.0" encoding="utf-8"?>
  24. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  25.     android:orientation="vertical"
  26.     android:layout_width="match_parent"
  27.     android:layout_height="match_parent">
  28.     <LinearLayout
  29.         android:id="@+id/frag"
  30.         android:layout_width="match_parent"
  31.         android:layout_height="match_parent"
  32.         android:orientation="horizontal">
  33.     </LinearLayout>
  34. </LinearLayout>    "type":参数类型值,<?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36.     android:orientation="vertical"
  37.     android:layout_width="match_parent"
  38.     android:layout_height="match_parent">
  39.     <LinearLayout
  40.         android:id="@+id/frag"
  41.         android:layout_width="match_parent"
  42.         android:layout_height="match_parent"
  43.         android:orientation="horizontal">
  44.     </LinearLayout>
  45. </LinearLayout>    "value":值<?xml version="1.0" encoding="utf-8"?>
  46. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.     android:orientation="vertical"
  48.     android:layout_width="match_parent"
  49.     android:layout_height="match_parent">
  50.     <LinearLayout
  51.         android:id="@+id/frag"
  52.         android:layout_width="match_parent"
  53.         android:layout_height="match_parent"
  54.         android:orientation="horizontal">
  55.     </LinearLayout>
  56. </LinearLayout>},<?xml version="1.0" encoding="utf-8"?>
  57. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  58.     android:orientation="vertical"
  59.     android:layout_width="match_parent"
  60.     android:layout_height="match_parent">
  61.     <LinearLayout
  62.         android:id="@+id/frag"
  63.         android:layout_width="match_parent"
  64.         android:layout_height="match_parent"
  65.         android:orientation="horizontal">
  66.     </LinearLayout>
  67. </LinearLayout>{<?xml version="1.0" encoding="utf-8"?>
  68. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  69.     android:orientation="vertical"
  70.     android:layout_width="match_parent"
  71.     android:layout_height="match_parent">
  72.     <LinearLayout
  73.         android:id="@+id/frag"
  74.         android:layout_width="match_parent"
  75.         android:layout_height="match_parent"
  76.         android:orientation="horizontal">
  77.     </LinearLayout>
  78. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  79. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  80.     android:orientation="vertical"
  81.     android:layout_width="match_parent"
  82.     android:layout_height="match_parent">
  83.     <LinearLayout
  84.         android:id="@+id/frag"
  85.         android:layout_width="match_parent"
  86.         android:layout_height="match_parent"
  87.         android:orientation="horizontal">
  88.     </LinearLayout>
  89. </LinearLayout>    }    ]}
复制代码
支持的参数类型列表
参数类型参数类型值boolean1int5double9string10示例:
  1. StageFragment fragment = new HiFragment();
  2. Bundle args = new Bundle();
  3. args.putString("params", "{"params":[{"key":"path","type":10,"value":"local"}]}");
  4. fragment.setArguments(args);
复制代码
应用工程Android逻辑部分的StageFragment

Fragment需要继承arkui_android_adapter.jar包所提供的StageFragment,StageFragment主要功能是将Android中Fragment的生命周期与OpenHarmony中UIAbility的生命周期进行映射,例如:
  1. package com.example.myapplication;import ohos.stage.ability.adapter.StageFragment;public class HiFragment extends StageFragment {    @Override    protected void onCreate(Bundle savedInstanceState) {<?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <LinearLayout
  7.         android:id="@+id/frag"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:orientation="horizontal">
  11.     </LinearLayout>
  12. </LinearLayout>super.setInstanceName("com.example.myapplication:entry:EntryAbility:");<?xml version="1.0" encoding="utf-8"?>
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  14.     android:orientation="vertical"
  15.     android:layout_width="match_parent"
  16.     android:layout_height="match_parent">
  17.     <LinearLayout
  18.         android:id="@+id/frag"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="match_parent"
  21.         android:orientation="horizontal">
  22.     </LinearLayout>
  23. </LinearLayout>super.onCreate(savedInstanceState);    }}
复制代码
为了将Fragment和UIAbility进行关联,需要重写StageFragment中的onCreate事件,在super.onCreate(savedInstanceState)之前设置instanceName,规则如下:
  1. bundleName:moduleName:abilityName:
复制代码
其中bundleName的值来自于OpenHarmony应用中app.json5配置文件,moduleName、abilityName的值来自于OpenHarmony应用中的module.json5配置文件。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册