I have an activity that have 3 fragment inside. I need restart a first fragment in my activity with click on Button.
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new HomeFragment(), "Home");
adapter.addFrag(new CategoryFragment(), "Category");
adapter.addFrag(new FilterFragment(), "Filter");
viewPager.setAdapter(adapter);
I test the following way but doesn't work and show error: refresh fragment at reload
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference
On button click you check if the current position of your ViewPager is bigger than 0 (first position) and set it to the first position, like this:
You can reload your fragment with this:
Being
instanceFragment
the instance of the fragment you wanna to reload andfragment_layout
theFrameLayout
component in your activity layout XML file.