Reputation: 639
Scenarios:
I have a Fragment with a ViewPager
, that it has a FragmentPagerAdapter
that provides 3 Fragments.
So the first time my application loads, the 3 tab fragments looks just fine.
When I navigate in the application, i do a FragmentManager.replace
to load another fragment.
When i go back, or navigate to the Main View (the tabbed View) my first and second tabs are blank.
I debugged the app, and the Fragment.onDestroyView
never gets call on the Tab fragment, so when it shows again, the Fragment.onCreateView
never gets called again, so my view is blank.
If I navigate to the third Tab in the Tabbed Fragment, then the Fragment.onDestroy
gets called by the PageAdapter
(i supposed because of the 1 offLimit on the ViewPager)
So, my question is, once another Fragment is loaded in my Activity, and then i go back, or navigate to the Tabbed View, how can i force the ViewPager
, or the PageAdapter
, or even the Fragment itself to be recreated.
So far i try to do a FragmentManager.remove(fragment)
with the TabView without any look, also, every time, i navigate to the Tabbed View i pass a new instance to the FragmentManager.replace
method, it seems that every time, he pulls the same object from the FragmentManager
.
TabView (Fragment):
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_view, container, false);
viewPager = (ViewPager) view.findViewById(R.id.view_iam);
final PagerAdapter pagerAdapter = new MyFragmentAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
return view;
}
PageAdapter:
private static class MyFragmentAdapter extends FragmentPagerAdapter {
private Fragment[] tabs;
private String[] names;
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
tabs = new Fragment[]{new FlightsView(), new MyFlightsRequestView(),
new MyFlightsView()};
names = new String[]{"VUELOS", "SOLICITUDES", "MIS VUELOS"};
}
@Override
public Fragment getItem(int position) {
return tabs[position];
}
@Override
public int getCount() {
return tabs.length;
}
@Override
public String getPageTitle(int position) {
return names[position];
}
}
FlightsView (One of the Tab Fragments)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_flight, container, false);
recycle_view = (RecyclerView) view.findViewById(R.id.recycler_view);
progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
setHasOptionsMenu(true);
return view;
}
Upvotes: 3
Views: 1278
Reputation: 237
Can you please try to extend your adapter from FragmentStatePagerAdapter instead of FragmentPagerAdapter
Upvotes: 7