LifecycleModel
The LifecycleModel class is designed to store and manage UI-related data in a lifecycle conscious way, the LifecycleModel class allows data to survive configuration changes such as screen rotations, it also handles the communication of the Activity / Fragment with the rest of the application, base on ViewModel
Introduction
LifecycleModel 基于 Google 在 2017 å¹´ I/O 大会上�?�布的 Android 架构组件ä¸çš„ ViewModel, �?�以帮助 Activity å’Œ Fragment 储å˜å’Œç®¡ï¿½?�一些与 UI 相关以�?�他们必需的数�?�, �?��?数�?�在�?幕旋转或�?置更改时�?�生的数�?�丢失, 还�?�以帮助开�?�者轻易实现 Fragment 与 Fragment 之间, Activity 与 Fragment 之间的通讯以�?�共享数�?�, å› ä¸ºæˆ‘çœ‹åˆ° Google 让 MVVM 框架ä¸çš„ ViewModel 具有了这些功能, 所以我也想让 MVP 框架ä¸çš„ Presenter, 乃至其他更多的模�?�都具有这些功能, 所以 LifecycleModel 诞生了
Lifecycle
Download
implementation 'me.jessyan:lifecyclemodel:1.0.1'Usage
Step 1
public class UserLifecycleModel implements LifecycleModel {
private int id;
public UserLifecycleModel(int id) {
this.id = id;
}
void doAction() {
}
@Override
public void onCleared() {
//release resources
}
}Step 2
//Put data
LifecycleModelProviders.of(activity/fragment).put(UserLifecycleModel.class.getName(), new UserLifecycleModel(1));
//Get data
LifecycleModelProviders.of(activity/fragment).get(UserLifecycleModel.class.getName());
//Remove data
LifecycleModelProviders.of(activity/fragment).remove(UserLifecycleModel.class.getName());Communication of the Activity / Fragment
public class UserLifecycleModel implements LifecycleModel {
private Subject<String, String> mSubject = PublishSubject.create();
public void doAction(String s) {
mSubject.onNext(s);
}
public void addAction(Action1<String> action) {
mSubject.subscribe(action);
}
}
public class AFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getActivity()).get(UserLifecycleModel.class.getName());
lifecycleModel.addAction(new Action1<String>() {
@Override
public void call(String s) {
// Update the UI.
}
});
}
}
public class BFragment extends Fragment {
@Override
public void onStart() {
super.onStart();
UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getActivity()).get(UserLifecycleModel.class.getName());
lifecycleModel.doAction("JessYan");
}
}About Me
- Email: [email protected]
- Home: http://jessyan.me
- 掘金: https://gold.xitu.io/user/57a9dbd9165abd0061714613
- 简书: http://www.jianshu.com/u/1d0c0bc634db
License
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.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

