Proper implementation of ViewPager2 in Android

UPDATE 7 Check : Migrate from ViewPager to ViewPager2 Check : Create swipe views with tabs using ViewPager2 UPDATE 6 Check out my answer if you want to implement Carousel using View Pager2 UPDATE 5 How to use TabLayout with ViewPager2 SAMPLE CODE Use below dependencies implementation ‘com.google.android.material:material:1.1.0-alpha08’ implementation ‘androidx.viewpager2:viewpager2:1.0.0-beta02’ SAMPLE CODE XMl layout <?xml … Read more

Access ViewPager Fragment method from Activity

Using the ViewPager.OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. You do that using the instantiateItem() method override in the adapter, here is a simplified example: class PagerAdapter extends FragmentPagerAdapter { String tabTitles[] … Read more

Getting the error “Java.lang.IllegalStateException Activity has been destroyed” when using tabs with ViewPager

This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on: … Read more

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are … Read more

Getting the current Fragment instance in the viewpager

by selecting an option, I need to update the fragment that is currently visible. A simple way of doing this is using a trick related to the FragmentPagerAdapter implementation: case R.id.addText: Fragment page = getSupportFragmentManager().findFragmentByTag(“android:switcher:” + R.id.pager + “:” + ViewPager.getCurrentItem()); // based on the current position you can then cast the page to the … Read more

Update data in ListFragment as part of ViewPager

Barkside’s answer works with FragmentPagerAdapter but doesn’t work with FragmentStatePagerAdapter, because it doesn’t set tags on fragments it passes to FragmentManager. With FragmentStatePagerAdapter it seems we can get by, using its instantiateItem(ViewGroup container, int position) call. It returns reference to fragment at position position. If FragmentStatePagerAdapter already holds reference to fragment in question, instantiateItem just … Read more

IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

Please check my answer here. Basically I just had to : @Override protected void onSaveInstanceState(Bundle outState) { //No call for super(). Bug on API Level > 11. } Don’t make the call to super() on the saveInstanceState method. This was messing things up… This is a known bug in the support package. If you need … Read more

tech