I have a FragmentPagerAdapter which holds several Fragments. The content of these Fragments change, but I did not get the Fragment to update in my update function. I already tried these suggestions without luck:
ViewPager PagerAdapter not updating the View
Android force Fragment to rebuild View
Here is a sniped of my code:
private void updateNavigation() {
//update code to redraw the fragment
//not workind
//mNavigationViewPager.getAdapter().notifyDataSetChanged();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle args = new Bundle();
AFragment newFragment = new AFragment();
args.putString("test");
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.navigation_pager, newFragment);
transaction.addToBackStack(null);
transaction.commit();
and this is the fragment:
public static class AFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment, draw layout dynamically
MyLinearLayout fl = new MyLinearLayout(getActivity());
Button btn = new Button(getActivity());
btn.setText(String.valueOf(cnt++));
btn.setGravity(Gravity.LEFT);
btn.setPadding(28, 7, 0, 0);
fl.addView(btn, new MyLinearLayout.LayoutParams(7, 5, 70, 50));
fl.setPadding(5, 5, 5, 5);
}
return fl;
}
}
activity on create: (..)
mNavigationViewPager = new ViewPager(this);
mNavigationViewPager.setId(R.id.navigation_pager);
mTabsAdapter = new TabsAdapter(this, mNavigationViewPager);
mTabsAdapter.addTab(actionBar.newTab().setText("text1"),
fragmentNr1, null);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
When I call
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
an an options menu interaction and afterwards do
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
in another options menu, the fragments gets updated correct.