Library for retaining Presenter on configuration change with minimal overhead. In case you don't know what Presenter is, check MVP.
If you are interesting how it works inside, please, check this artcile.
You can find an example in app
module. There you can find following cases:
- Single Activity: Retaining presenter for activity without fragments.
- Activity with Fragment: Retaining presenter for activity and fragment inside.
- Fragment Navigation: Retaining presenter for navigation with fragments. When you replace fragment with new one, old presenter is destroyed.
- Nested Fragments inside ViewPager: Activity contains fragment with nested fragments inside viewpager.
- Just extends
PresenterActivity
orPresenterFragment
, depending on component you want to work with. - Override methods
onCreatePresenter()
andgetPresenterView()
- Enjoy.
NB Activity which holds PresenterFragment
must extends PresenterActivity
. If it doesn't need Presenter, just skip step 2.
onAttachView(View)
Called to attach view to the Presenter.onDetachView()
Called to detach view from the Presenter.onDestroy()
Called when presenter is going to be destroyed.
- In case you don't want to retain Presenter override
retainPresenter()
and return false. - If you want to reset Presenter state after configuration change, do that in
onPresenterRestored()
in Fragment or Activity. - DO NOT override method
onRetainCustomNonConfigurationInstance()
. UseonRetainCustomNonConfigurationObject()
instead. You can retrieve object later fromgetLastCustomNonConfigurationObject()
method.
dependencies {
compile 'org.kaerdan.presenterretainer:presenterretainer:0.2.2'
}
The MIT License (MIT)