From fa5896ee5bfda7a47d04c856767f14e02a0677ba Mon Sep 17 00:00:00 2001 From: Ritvik Saraf <13ritvik@gmail.com> Date: Fri, 19 Oct 2018 18:44:03 +0530 Subject: [PATCH] fixed screen rotation for viewpager --- .../newpipe/fragments/detail/TabAdaptor.java | 28 ++++++------------- .../list/videos/RelatedVideosFragment.java | 2 -- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java index 3ed247e50..27cc3ec8a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java @@ -3,6 +3,7 @@ package org.schabi.newpipe.fragments.detail; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; @@ -11,10 +12,11 @@ public class TabAdaptor extends FragmentPagerAdapter { private final List mFragmentList = new ArrayList<>(); private final List mFragmentTitleList = new ArrayList<>(); - int baseId = 0; + private final FragmentManager fragmentManager; public TabAdaptor(FragmentManager fm) { super(fm); + this.fragmentManager = fm; } @Override @@ -27,12 +29,6 @@ public class TabAdaptor extends FragmentPagerAdapter { return mFragmentList.size(); } - @Override - public long getItemId(int position) { - // give an ID different from position when position has been changed - return baseId + position; - } - public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); @@ -65,19 +61,13 @@ public class TabAdaptor extends FragmentPagerAdapter { else return POSITION_NONE; } - /** - * Notify that the position of a fragment has been changed. - * Create a new ID for each position to force recreation of the fragment - * @param n number of items which have been changed - */ - public void notifyChangeInPosition(int n) { - // shift the ID returned by getItemId outside the range of all previous fragments - // https://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android - baseId += getCount() + n; - } - public void notifyDataSetUpdate(){ - notifyChangeInPosition(1); notifyDataSetChanged(); } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + fragmentManager.beginTransaction().remove((Fragment) object).commitNowAllowingStateLoss(); + } + } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java index 44c7c6787..694731c69 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java @@ -37,8 +37,6 @@ public class RelatedVideosFragment extends BaseListInfoFragment