Android MVP - 拥有完整生命周期的Presenter,支持Multi P

结合自己对MVP的理解,加上个人习惯,整理出这么一份Android MVP框架

  • 通过注解动态注入Presenter
  • Presenter拥有完整生命周期
  • 支持多Presenter注入
  • 状态复原

0x00 前言

此框架可以说是Fork nucleus 而来,只不过结合了个人理解和习惯进行更改,放上此框架链接
https://github.com/izyhang/Damon/tree/feature/multi-presenter

0x01 介绍

先介绍一下Damon的简单使用

新建View、Presenter

Presenter具有与Activity一样的生命周期,这里展示部分生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
interface MainView {
fun log(msg: String)
}

class MainPresenter : BasePresenter<MainView>() {
override fun onCreate(arguments: Bundle?, savedState: Bundle?) {
super.onCreate(arguments, savedState)
view.log("MainPresenter.onCreate")
}

override fun onResume() {
super.onResume()
view.log("MainPresenter.onResume")
}

override fun onPause() {
super.onPause()
view.log("MainPresenter.onPause")
}

override fun onDestroy() {
super.onDestroy()
view.log("MainPresenter.onDestroy")
}
}

修改MainActivity

MainActivity绑定Presenter,想要拿到Presenter通过@BindPresenter获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RequiresPresenter(MainPresenter::class)
class MainActivity : BaseActivity(), MainView {
@BindPresenter
private var mainPresenter: MainPresenter? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainPresenter.xxx()
}

override fun log(msg: String) {
println(msg)
}
}

多Presenter注入

1
2
3
4
5
6
7
8
9
10
11
@RequiresPresenter(value = [MainPresenter::class, SecondPresenter::class])
class MainActivity : BaseActivity(), MainView, SecondView {
@BindPresenter
private var mainPresenter: MainPresenter? = null
@BindPresenter
private var secondPresenter: SecondPresenter? = null

override fun log(msg: String) {
println(msg)
}
}

0x02 工作原理

下面介绍一下Damon的工作原理

Presenter的动态注入和赋值

用注解代替手动实例化Presenter更加方便,并且在封装好的Activity实现了状态恢复,避免重复创建Presenter

在界面创建之时会传入当前classReflectionPresenterFactory,通过cls.getAnnotation拿到@RequiresPresenter注解并保存,在这里也对当前class@BindPresenter注解的成员进行保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Nullable
public static ReflectionPresenterFactory fromViewClass(Object host, Class<?> cls) {
RequiresPresenter annotation = cls.getAnnotation(RequiresPresenter.class);
Class<? extends MvpPresenter>[] pClass = null != annotation ? annotation.value() : null;
if (null == pClass) {
return null;
}
List<Field> fields = new ArrayList<>();
for (Field field : cls.getDeclaredFields()) {
Annotation[] annotations = field.getDeclaredAnnotations();
if (annotations.length < 1) {
continue;
}
if (annotations[0] instanceof BindPresenter) {
fields.add(field);
}
}
return new ReflectionPresenterFactory(host, pClass, fields);
}

Presenter的创建

上面讲过DamonPresenter是具有完整生命周期的,看到封装的Activity MvpAppCompatActivity.java

Activity的生命周期通过PresenterLifecycleDelegate去控制Presenter的方法调用,赋予其生命周期。其中还重载了onSaveInstanceState辅助Presenter状态恢复机制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class MvpAppCompatActivity extends AppCompatActivity implements MvpView {

private static final String PRESENTER_STATE_KEY = "presenter_state";
private PresenterLifecycleDelegate mPresenterDelegate =
new PresenterLifecycleDelegate(ReflectionPresenterFactory.fromViewClass(this, getClass()));

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenterDelegate.onCreate(this, getIntent().getExtras(), null != savedInstanceState ? savedInstanceState.getBundle(PRESENTER_STATE_KEY) : null);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(PRESENTER_STATE_KEY, mPresenterDelegate.onSaveInstanceState());
}

@Override
public void onStart() {
super.onStart();
mPresenterDelegate.onStart();
}

@Override
public void onResume() {
super.onResume();
mPresenterDelegate.onResume();
}

@Override
public void onPause() {
mPresenterDelegate.onPause();
super.onPause();
}

@Override
public void onStop() {
mPresenterDelegate.onStop();
super.onStop();
}

@Override
public void onDestroy() {
mPresenterDelegate.onDestroy(!isChangingConfigurations());
super.onDestroy();
}
}

PresenterLifecycleDelegate

最重要的就是此类,连接ActivityPresenter的枢纽,控制Presenter的创建、恢复、生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
public class PresenterLifecycleDelegate {

private static final String PRESENTER_KEY = "presenter - ";
private static final String PRESENTER_ID_KEYS = "presenter_ids";

@Nullable
private PresenterFactory mPresenterFactory;
@Nullable
private List<? extends MvpPresenter> mPresenters;

private boolean mPresenterHasView;

public PresenterLifecycleDelegate(@Nullable PresenterFactory presenterFactory) {
this.mPresenterFactory = presenterFactory;
}

public void onCreate(MvpView view, @Nullable Bundle arguments, @Nullable Bundle savedState) {
if (mPresenterFactory == null) {
return;
}
Bundle presenterBundle = null;
if (savedState != null) {
presenterBundle = ParcelFn.unmarshall(ParcelFn.marshall(savedState));
}
createPresenter(presenterBundle);
if (mPresenters != null && !mPresenters.isEmpty()) {
mPresenterFactory.bindPresenter(mPresenters);
for (MvpPresenter presenter : mPresenters) {
//noinspection unchecked
presenter.create(view, arguments, null != presenterBundle ? presenterBundle.getBundle(PRESENTER_KEY.concat(presenter.getClass().getSimpleName())) : null);
}
}
}

private void createPresenter(Bundle presenterBundle) {
if (presenterBundle != null) {
mPresenters = PresenterStorage.INSTANCE.getPresenter(presenterBundle.getStringArray(PRESENTER_ID_KEYS));
}

if (mPresenters == null) {
//noinspection ConstantConditions
mPresenters = mPresenterFactory.createPresenter();
PresenterStorage.INSTANCE.add(mPresenters);
}
}

public void onStart() {
if (mPresenters != null && !mPresenters.isEmpty()) {
for (MvpPresenter presenter : mPresenters) {
presenter.start();
}
}
}

public Bundle onSaveInstanceState() {
Bundle bundle = new Bundle();
if (mPresenters != null && !mPresenters.isEmpty()) {
String[] ids = new String[mPresenters.size()];
for (MvpPresenter presenter : mPresenters) {
Bundle presenterBundle = new Bundle();
presenter.save(presenterBundle);
bundle.putBundle(PRESENTER_KEY.concat(presenter.getClass().getSimpleName()), presenterBundle);

ids[mPresenters.indexOf(presenter)] = PresenterStorage.INSTANCE.getId(presenter);
}
bundle.putStringArray(PRESENTER_ID_KEYS, ids);
}
return bundle;
}

public void onResume() {
if (mPresenters != null && !mPresenters.isEmpty() && !mPresenterHasView) {
for (MvpPresenter presenter : mPresenters) {
presenter.resume();
}
mPresenterHasView = true;
}
}

public void onPause() {
if (mPresenters != null && !mPresenters.isEmpty() && mPresenterHasView) {
for (MvpPresenter presenter : mPresenters) {
presenter.pause();
}
mPresenterHasView = false;
}
}

public void onStop() {
if (mPresenters != null && !mPresenters.isEmpty()) {
for (MvpPresenter presenter : mPresenters) {
presenter.stop();
}
}
}

public void onDestroy(boolean isFinal) {
if (isFinal && mPresenters != null && !mPresenters.isEmpty()) {
for (MvpPresenter presenter : mPresenters) {
presenter.destroy();
}
mPresenters.clear();
mPresenters = null;
}
}
}

0x03 讲在最后

该框架实现的Presenter恢复机制,存储的id不止是单纯的Presenter类名,大可不必担心Presenter复用混乱的情况,具体可以看 PresenterStorage.java 的实现

此次说讲Multi Presenter版本的Damon是新变支的2.0.0-alpha,未经过大量测试。
不过此前的Single Presenter版本的Damon,版本号1.1.0,则在开发环境稳定运行许久

最后再放上项目链接https://github.com/izyhang/Damon/tree/feature/multi-presenter

欢迎大家指出不足、留下您的意见~