本文介绍将ArkUI框架的UIAbility跨平台部署至Android平台Fragment的使用说明,实现Android原生Fragment和ArkUI跨平台Fragment的混合开发,方便开发者灵活部署跨平台界面。
Android工程配置
Android工程的PackageName需要与OpenHarmony工程的BundleName一致;
请在Android应用的gradle.properties文件,使能AndroidX:请在Android应用的build.gradle文件增加AndroidX Fragment库的依赖项:- dependencies {
- implementation 'androidx.appcompat:appcompat:1.4.1'
- }
复制代码 ArkUI-X和Android平台集成所用关键类
应用工程Android逻辑部分的StageApplication
应用需要继承arkui_android_adapter.jar包所提供的StageApplication。StageApplication用于初始化资源路径以及加载配置信息,例如:- package com.example.myapplication;
- import ohos.stage.ability.adapter.StageApplication;
- public class MyApplication extends StageApplication {
-
- }
复制代码 应用工程Android逻辑部分Fragment的宿主Activity
原生Activity需要继承androidx.fragment.app.FragmentActivity,绑定StageFragment示例如下:- package com.example.myapplication;
- import android.os.Bundle;
- import androidx.fragment.app.FragmentActivity;
- import androidx.fragment.app.FragmentManager;
- import ohos.stage.ability.adapter.StageFragment;
- public class MainActivity extends FragmentActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>super.onCreate(savedInstanceState);
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>setContentView(R.layout.activity_main);
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>StageFragment fragment = new HiFragment();
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>FragmentManager manager = getSupportFragmentManager();
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>manager.beginTransaction().add(R.id.frag,fragment).commit();
- }
- }
复制代码 其中activity_main.xml文件示例如下:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>
复制代码 如果当前StageFragment对应的UIAbility涉及页面跳转,Activity需要重写onBackPressed方法,以便在手机back键点击或手势侧滑时逐级返回页面,否则会退出跨平台承载的Activity页面。- @Overridepublic void onBackPressed() { if(fragment.onBackPressed()) {<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>super.onBackPressed(); }}
复制代码 参数传递
StageFragment支持传递参数,参数的传递需要通过setArguments()进行设置,规则如下:
key值为params
value为json格式支持的参数类型列表
参数类型参数类型值boolean1int5double9string10示例:- StageFragment fragment = new HiFragment();
- Bundle args = new Bundle();
- args.putString("params", "{"params":[{"key":"path","type":10,"value":"local"}]}");
- fragment.setArguments(args);
复制代码 应用工程Android逻辑部分的StageFragment
Fragment需要继承arkui_android_adapter.jar包所提供的StageFragment,StageFragment主要功能是将Android中Fragment的生命周期与OpenHarmony中UIAbility的生命周期进行映射,例如:- 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"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>super.setInstanceName("com.example.myapplication:entry:EntryAbility:");<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>super.onCreate(savedInstanceState); }}
复制代码 为了将Fragment和UIAbility进行关联,需要重写StageFragment中的onCreate事件,在super.onCreate(savedInstanceState)之前设置instanceName,规则如下:- bundleName:moduleName:abilityName:
复制代码 其中bundleName的值来自于OpenHarmony应用中app.json5配置文件,moduleName、abilityName的值来自于OpenHarmony应用中的module.json5配置文件。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |