I am trying to set up a tab with MVP and have some difficulties. I use the Google Dagger MVP sample application as a shared link, and they store a link to each presenter in every action. However, in my tabbed layout, I use one action with a snippet for each tab (supplied through the FragmentPagerAdapter). These fragments are created, and their presenters are introduced using the FragmentPagerAdapter getItem () method. The problem is that when the application is rotated, my fragments are automatically restored, but do not return their presenters, and then fly out due to NPE. I save presenters as fields in activity or FragmentPagerAdapter (a la Google), but none of them work, and this seems like a hacker anyway.
This is basically how I do it in the FragmentPagerAdapter now:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
@Inject MyPresenter mMyPresenter;
@Override
public Fragment getItem (int position){
switch (position) {
case MY_FRAGMENT_INDEX:
return createMyFragment;
case OTHER_FRAGMENT_INDEX:
return createMyOtherFragment;
default:
return null;
}
private Fragment createMyFragment() {
MyFragment myFragment = MyFragment.newInstance();
DaggerMyPresenterComponent.builder()
.MyModelComponent(
((MyApplication) getApplication()).getMyModelComponent())
.MyPresenterModule(new MyPresenterModule(myFragment)).build()
.inject(this);
return myFragment;
}
? ( ) ( FragmentPagerAdapter )?