Save fragments state that is in a viewPager

0

WheneverthedeviceisrotatedIamlosingthecontentsofthefragmentsthatareinthreetabs,IalreadymadeuseoftheonSaveInstanceStateinActivitybutnowIamtakingabathbecauseofmymainActivityIamcallingaMainfragementthisinturnrecoversthethreeobjects

privateTabLayoutmTabLayout;privateSectionsPagerAdaptermSectionsPagerAdapter;privateViewPagermViewPager;

p>

I'mtryingtochangetheadaptertosolvetheproblem,stilllookingforreferencesinthedocumentationifanyonecangiveaforce

Hereismyadapter

classSectionsPagerAdapterextendsFragmentPagerAdapter{privateFragment[]currentFragment;privateString[]mTabTiles;privateContextmContext;publicSectionsPagerAdapter(FragmentManagerfm,Contextcontext,String[]tabTiles){super(fm);this.mTabTiles=tabTiles;this.mContext=context;this.currentFragment=newFragment[this.mTabTiles.length];}@OverridepublicFragmentgetItem(intposition){Fragmentfrag=null;if(position==0){frag=newFragmentA();}elseif(position==1){frag=newFragmentB();}elseif(position==2){frag=newFragmentC();}currentFragment[position]=frag;Bundleb=newBundle();b.putInt("position",position);

     frag.setArguments(b);

    return frag;
   }

   @Override
   public int getCount() {
       return this.mTabTiles.length;
   }

   @Override
   public CharSequence getPageTitle(int position) {

       return this.mTabTiles[position] ;
   }

}

and MainFragment

public class MainFragment extends Fragment {

   private static final String TAG = "MainFragment";

   private TabLayout mTabLayout;
   private SectionsPagerAdapter mSectionsPagerAdapter;
   private ViewPager mViewPager;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_main, container, false);

       Log.i(TAG, "onCreateView()");

       mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), getActivity(), getResources().getStringArray(R.array.tab_titulo));

       mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
       // Set up the ViewPager with the sections adapter.
       mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
       mViewPager.setAdapter(mSectionsPagerAdapter);
       mTabLayout.setupWithViewPager(mViewPager);

       return view;
}


   @Override
   public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);

       Log.i(TAG, "onSaveInstanceState");

       //outState.putInt("currentTab", mTabLayout.getCurrentTab());
       outState.putInt("position", mTabLayout.getSelectedTabPosition());
       outState.putInt("currentPage", mViewPager.getCurrentItem());
   }

   @Override
   public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
       super.onViewStateRestored(savedInstanceState);

       Log.i(TAG, "onViewStateRestored");

       if(savedInstanceState!=null) {

           Log.i(TAG, "onViewStateRestored -> Posição: " +    savedInstanceState.getInt("position"));

           mViewPager.setCurrentItem(savedInstanceState.getInt("position"));

           Log.i(TAG, "onViewStateRestored -> Posição: " + savedInstanceState.getInt("position") + " -> mViewPager.setCurrentItem()");

         mSectionsPagerAdapter.getItem(savedInstanceState.getInt("position"));

           Log.i(TAG, "onViewStateRestored -> Posição: " + savedInstanceState.getInt("position") + " -> mSectionsPagerAdapter.getItem()");

       }
   }

What I was able to search I updated the onSaveInstanceState method and created I created the onViewStateRestored in the MainFragment class that I already updated in the code above, although this change did not give the expected result, I am calling the ViewPage Adapter with the position of the Tab and I notice in logcat the events when I rotate the device

// Salvando os estados
I/MainFragment: onSaveInstanceState
I/FragmentA: onSaveInstanceState()
I/FragmentB: onSaveInstanceState()


I/MainFragment: onCreateView()
// Nova instancia do adaptador
I/SectionsPagerAdapter: SectionsPagerAdapter()
// Aqui vejo que foi obtida a posição 0 da Tab ou seja a primeira Tab 
I/MainFragment: onViewStateRestored
I/MainFragment: onViewStateRestored -> Posição: 0
I/MainFragment: onViewStateRestored -> Posição: 0 ->     mViewPager.setCurrentItem()
I/SectionsPagerAdapter: getItem( 0)
I/MainFragment: onViewStateRestored -> Posição: 0 ->    mSectionsPagerAdapter.getItem()
I/FragmentA: onCreateView()
I/FragmentA: onActivityCreated()

I/MainFragment: onCreateView()
I/SectionsPagerAdapter: SectionsPagerAdapter()
I/MainFragment: onViewStateRestored
    
asked by anonymous 21.03.2017 / 19:39

1 answer

0

I made another project in which I make use of tabs with TabLayout, ViewPager calling from an activity with this approach the positioning of the device did not cause any problem in the view everything worked correctly, then concludes that I have migrated the tablayout and viewpage objects for a fragment was what caused the behavior.

    
24.03.2017 / 18:08