/Silute

MVP development library based on Loader

Primary LanguageJava

Silute

基于Loader的MVP开发库

由于在Configuration Changes的时候,Activity会遭遇重启,这时候Presenter不应该被销毁,如果 Presenter受到Activity的生命周期控制,由于之前的Activity已经不存在,Presenter也就相应的不存在了。 保存Presenter实例,使用Loader是一种思路,Silute的特点如下

  • Loader有自己的生命周期,Activity/Fragment都能持有Loader的引用,并且LoaderConfiguration Changes的时候不会销毁, 在Activity重启的时候,能重新连接。
这点和调用了setRetainInstance(true)的Fragment很像,官方API Guides也提到
如果需要恢复大量的数据,Configuration Changes的时候是有必要使Activity重启的,这时候可以使用setRetainInstance(true)的Fragment
来保存大量的不会造成内存泄漏的数据。其中调用了setRetainInstance(true)的Fragment在Configuration Changes的时候不会销毁
  • PresenterModelView都通过接口进行交互,Silute封装了大部分Model-View-Presenter绑定的代码,使用起来非常简单
  • Activity的生命周期内回调LifecycleDelegate对应的方法,可以自己完成具体的实现类
  • Presenter拥有Activity的生命周期的方法,可以在Activity对应的生命周期内做相应的操作
  • BaseActivity里面的Presenter有关的方法是选择性的,一些简单的页面不需要使用MVP的时候,不用重写

依赖

compile 'com.skylinetan.silute:silute:1.0.0'

使用方法

推荐的使用方法

1.View层接口继承自IViewPresenter层接口继承自IPresenter

2.Activity继承自抽象类SilBaseActivityFragment继承自SilBaseFragment,其中选择性重写的方法有:

  • createPresenterFactory:Presenter的工厂类,返回一个BasePresenterFactory实例
  • getLoaderId:返回Loaderid,一个id对应一个Loader

3.Presenter继承BasePresenter实现Presenter层接口,Activity实现View层接口,BasePresenterfactory里面保存的是Activity的弱引用

License

MIT License

Copyright (c) 2016-2018 skylineTan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.