結合自己對MVP
的理解,加上個人習慣,整理出這麼一份Android MVP
框架
- 通過註解動態注入Presenter
- Presenter擁有完整生命週期
- 支援多Presenter注入
- 狀態復原
0x00 前言
此框架可以說是Fork
nucleus 而來,只不過結合了個人理解和習慣進行更改,放上此框架連結
github.com/izyhang/Dam…
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的動態注入和賦值
用註解代替手動例項化Presenter
更加方便,並且在封裝好的Activity
實現了狀態恢復,避免重複建立Presenter
在介面建立之時會傳入當前class
到ReflectionPresenterFactory
,通過cls.getAnnotation
拿到@RequiresPresenter
註解並儲存,在這裡也對當前class
有@BindPresenter
註解的成員進行儲存
@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的建立
上面講過Damon
的Presenter
是具有完整生命週期的,看到封裝的Activity
MvpAppCompatActivity.java
在Activity
的生命週期通過PresenterLifecycleDelegate
去控制Presenter
的方法呼叫,賦予其生命週期。其中還過載了onSaveInstanceState
輔助Presenter
狀態恢復機制
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
的建立、恢復、生命週期
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
,則在開發環境穩定執行許久
最後再放上專案連結github.com/izyhang/Dam…
歡迎大家指出不足、留下您的意見~