結(jié)合自己對MVP
的理解领突,加上個人習(xí)慣险耀,整理出這么一份Android MVP
框架
- 通過注解動態(tài)注入Presenter
- Presenter擁有完整生命周期
- 支持多Presenter注入
- 狀態(tài)復(fù)原
0x00 前言
此框架可以說是Fork
nucleus 而來,只不過結(jié)合了個人理解和習(xí)慣進(jìn)行更改则拷,放上此框架鏈接
https://github.com/izyhang/Damon/tree/feature/multi-presenter
0x01 介紹
先介紹一下Damon
的簡單使用
新建View贡蓖、Presenter
Presenter
具有與Activity
一樣的生命周期,這里展示部分生命周期
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
獲取
@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注入
@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的動態(tài)注入和賦值
用注解代替手動實(shí)例化Presenter
更加方便斥铺,并且在封裝好的Activity
實(shí)現(xiàn)了狀態(tài)恢復(fù),避免重復(fù)創(chuàng)建Presenter
在界面創(chuàng)建之時會傳入當(dāng)前class
到ReflectionPresenterFactory
坛善,通過cls.getAnnotation
拿到@RequiresPresenter
注解并保存晾蜘,在這里也對當(dāng)前class
有@BindPresenter
注解的成員進(jìn)行保存
@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的創(chuàng)建
上面講過Damon
的Presenter
是具有完整生命周期的,看到封裝的Activity
MvpAppCompatActivity.java
在Activity
的生命周期通過PresenterLifecycleDelegate
去控制Presenter
的方法調(diào)用眠屎,賦予其生命周期剔交。其中還重載了onSaveInstanceState
輔助Presenter
狀態(tài)恢復(fù)機(jī)制
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
最重要的就是此類,連接Activity
和Presenter
的樞紐改衩,控制Presenter
的創(chuàng)建岖常、恢復(fù)、生命周期
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 講在最后
該框架實(shí)現(xiàn)的Presenter
恢復(fù)機(jī)制葫督,存儲的id
不止是單純的Presenter
類名竭鞍,大可不必?fù)?dān)心Presenter
復(fù)用混亂的情況,具體可以看 PresenterStorage.java 的實(shí)現(xiàn)
此次說講
Multi Presenter
版本的Damon
是新變支的2.0.0-alpha
候衍,未經(jīng)過大量測試笼蛛。
不過此前的Single Presenter
版本的Damon
洒放,版本號1.1.0
蛉鹿,則在開發(fā)環(huán)境穩(wěn)定運(yùn)行許久
最后再放上項(xiàng)目鏈接https://github.com/izyhang/Damon/tree/feature/multi-presenter
歡迎大家指出不足、留下您的意見~