# LifecycleModel **Repository Path**: cmzl_ohos/lifecycle-model ## Basic Information - **Project Name**: LifecycleModel - **Description**: This library is designed to store and manage UI-related data in a lifecycle conscious way. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-06-21 - **Last Updated**: 2021-08-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LifecycleModel #### 项目介绍 LifecycleModel 实现了 Fraction 与 Fraction 之间, Ability 与 Fraction 之间的通讯以及共享数据。 ![overview](screenShoot/move.gif) #### 集成 ##### 方法1: 直接使用har包 ``` 通过library生成har包,添加har包到要集成的libs文件夹内 在entry的gradle内添加如下代码 implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) ``` ##### 方法2: gradle依赖 ``` allprojects{ repositories{ mavenCentral() } } implementation "com.gitee.archermind-ti:lifecyclemodel:1.0.0-beta2" ``` #### 使用说明 1. 步骤一 ``` public class UserLifecycleModel implements LifecycleModel { private int id; public UserLifecycleModel(int id) { this.id = id; } void doAction() { } @Override public void onCleared() { //release resources } } ``` 2. 步骤二 ``` //Put data LifecycleModelProviders.of((FractionAbility) getAbility(),ResourceTable.Id_stack_layout).put(UserLifecycleModel.class.getName(), new UserLifecycleModel()); //Get data LifecycleModelProviders.of((FractionAbility) getAbility(),ResourceTable.Id_stack_layout).get(UserLifecycleModel.class.getName()) //Remove data LifecycleModelProviders.of((FractionAbility) getAbility(),ResourceTable.Id_stack_layout).remove(UserLifecycleModel.class.getName()); ``` 3. Fraction的通信 ``` public class UserLifecycleModel implements LifecycleModel { private Subject mSubject = PublishSubject.create(); public void doAction(String s) { mSubject.onNext(s); } public void addAction(Action1 action) { mSubject.subscribe(action); } } public class AFraction extends Fraction { @Override public void onStart(Intent intent) { super.onStart(intent); UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getFractionAbility(),id).get(UserLifecycleModel.class.getName()); lifecycleModel.addAction(new Action1() { @Override public void call(String s) { // Update the UI. } }); } } public class BFraction extends Fraction { public void sendFraction() { UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getFractionAbility(),id).get(UserLifecycleModel.class.getName()); lifecycleModel.doAction("ohos"); } } ``` #### 版权和许可信息 ``` Copyright 2017, jessyan Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```