In my application I have 3 fragments in a viewPager.
For the first fragment I would like to reload the view after a button press (basically call again the onCreateView()
).
To get this functionality I decided to use FragmentTransaction
, detach()
, attach()
and commit()
the fragment.
In fragment I have created SetData()
method:
public void setData() {
Fragment frg = null;
frg = mainMenu.getFragmentByPosition();
final android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
In the activity which holds the viewPager I have getFragmentByPosition()
method:
public Fragment getFragmentByPosition() {
String tag = "android:switcher:" + R.id.viewpager + ":" + viewPager.getCurrentItem();
return getSupportFragmentManager().findFragmentByTag(tag);
}
The problem is that I get NullPointerException in getFragmentByPosition()
UPDATE
The problem is that I cannot get the reference to a currently shown fragment.
adapter.getItem(1)
- returns null
getFragmentManager().getFragments().get(0)
- returns null