Android - MVP - Fragments,Presenter和ViewPager

时间:2018-06-06 19:49:40

标签: android android-fragments android-viewpager mvp

我有一个Activity1,它通过ViewPager包含多个片段。 每个片段都有一个Presenter。出于某种原因,当我旋转屏幕时,片段被重新创建并失去与演示者的关系

我正在做以下

  1. 打开活动1(包含带片段的viewPager)
  2. open activity 2
  3. 旋转屏幕
  4. 按后退按钮
  5. 显然片段已重新创建,我收到以下错误

      

    引起:java.lang.NullPointerException:尝试调用   接口方法'void ....在null对象上的Presenter1.subscribe()'   引用.... Fragment1.onResume(Fragment1.java:72)

    我的 Activity1包含2个视图(layout和layout-normal-land) 活动1的代码如下

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        final ViewPager viewPager = (ViewPager) findViewById(R.id.htab_viewpager);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.htab_tabs);
        ...
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    

    ViewPagerAdapter类的代码如下:

    private  class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<String> mFragmentTitleList = Arrays.asList("Fragment1", "Fragment2");
    
        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }
    
        @Override
        public Fragment getItem(int position) {
            if (position == 0){
                Fragment1 fragment = Fragment1.newInstance();
                new Presenter1(fragment);
                return fragment;
    
            }else if (position == 1){                
                Fragment2 fragment = Fragment2.newInstance();
                new Presenter2(fragment);
                return fragment;
    
            }
            return null;
        }
    
         @Override
        public int getCount() {
            return mFragmentTitleList.size();
        }
    
    
    
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
    

    每个片段的代码如下

    public class Fragment1 extends Fragment {
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Retain this fragment across configuration changes.
            setRetainInstance(true);
        }
    
        @Override
        public void onResume() {
            super.onResume();
            mPresenter.subscribe();
        }
    
        @Override
        public void onPause() {
            super.onPause();
            mPresenter.unsubscribe();
        }
    
    
       @Override
       public void setPresenter(@NonNull Presenter1 presenter) {
            mPresenter = checkNotNull(presenter);
        }
    
        ....
    
    }
    

    每个presener的代码

    public class Presenter1 {
    
        private final Fragment1 view;
    
        public PerfilAlumnoCalificacionesPresenter(Fragment1 view ....) {
            ....
            this.view = view
            this.view.setPresenter(this);
        }
    
        ....
    
    }
    

    我该如何解决这个问题?我不明白为什么片段重新创建,如果它包含setRetainInstance(true)

    初始化Presenter的最佳方法是什么,以便碎片不会失去关系?

    谢谢,对不起我的英文

0 个答案:

没有答案