0
Whenever the device is rotated I’m losing the content of Fragments that are in three tabs, I’ve already made use of onSaveInstanceState in Activity more now I’m taking a bath because of my main Activity I’m calling a Mainfragement this in turn recovers the three objects
private TabLayout mTabLayout;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
and inflates the view that has three Fragments, the A, B and C, initially I tried to make the recovery in each Fragment more is not this because I keep losing the data of each view so I have to act on Mainfragment
I’m trying to change the adapter to solve the problem, still looking for references in the documentation if someone can give a force
Here is my adapter
class SectionsPagerAdapter extends FragmentPagerAdapter {
private Fragment[] currentFragment ;
private String[] mTabTiles;
private Context mContext;
public SectionsPagerAdapter(FragmentManager fm, Context context, String[] tabTiles) {
super(fm);
this.mTabTiles = tabTiles;
this.mContext = context;
this.currentFragment = new Fragment[this.mTabTiles.length];
}
@Override
public Fragment getItem(int position) {
Fragment frag = null;
if (position==0){
frag = new FragmentA();
} else if(position==1){
frag = new FragmentB();
} else if(position==2){
frag = new FragmentC();
}
currentFragment[position] = frag;
Bundle b = new Bundle();
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 the 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 research I updated the method onSaveInstanceState and created onViewStateRestored in the Mainfragment class that I have 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 observe in the 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
Be more explicit about which values you want to keep.
– ramaral
Hello Ramaral, so I need when rotate the device the information I have in the Tabs continue only this. When this occurs I have to select Tab by Tab one by one for its content to appear.
– Robson
I don’t understand. If by selecting the tab the content appears it is because it has been saved.
– ramaral
When I select the Tab manually yes of course this appears because the Adapter ( Sectionspagerarpter ) is called and executes getItem(). What I want and I believe is this behavior of every app is when I access an Activity and rotate the device Android rebuilt the Screen, so using the methods to save I can restore the screen and this is my intention, I will update what I got researching these days.
– Robson
I did an update of the Mainfragment class, I also updated my question with the event log, although I am searching the Tablayout and trying to save the getSelectedTabPosition() position very much expected viewPager.setCurrentItem(savedInstanceState.getInt(position)) could solve the problem and restore the data when I rotated the device because that’s what I understood, more I’m in the search
– Robson
When you say "no show" when you rotate the device, does that mean the screen is blank? Why do you have Tablayout in the layout of a Fragment and not Activity?
– ramaral
I put an image of the rotated screen I believe that now give to exemplify what is occurring, in the portrait orientation all tabs had content qdo I rotate they disappear even I saving and recovering the state.
– Robson
This should not happen. Views are automatically retrieved by the system. The only strange fact (never seen like this) is that Tablayout is in the layout of a Fragment and not in Activity, could be the reason for this anomalous behavior.
– ramaral
ramaral, the reason to be in a Fragment is the project was created with Navigation Drawer, the main activity contained to Tabs and Viewpager, only that I put other activities in the menu of Drawer and calling these I missed the Menu Drawer, got ugly so I turned everything into Fragments and what was in the main Activity the tabs I passed to a fragmentMain, at the beginning of the project I did not test the rotation of the device but I will do it going back the code.
– Robson
Create an Activity with Drawer, then inherit the other activities from Drawer.
– ramaral
I did a test I created another project, and without Fragments in the main activity, Fragments are only for Tabs, in the xml of Activity Viewpage and really everything worked as it should and I didn’t even need to save state of anything in Activity, anything, ie the roll occurred when I "migrated" the Tabs and the Viewpage for a Fragment, then ramaral you this right quado said "This shouldn’t happen".
– Robson