From c929f00456285d7548ef54852ebb14bf10b948c9 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 23 Oct 2022 21:28:54 +0200 Subject: [PATCH] fix: remember selected channel tab on screen rotation --- .../list/channel/ChannelFragment.java | 27 ++++++++++++++++--- .../list/channel/ChannelInfoFragment.java | 2 +- .../list/channel/ChannelTabFragment.java | 2 +- .../list/channel/ChannelVideosFragment.java | 2 +- .../org/schabi/newpipe/util/ChannelTabs.java | 5 +++- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index f71791d8e..51625d202 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -16,6 +16,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.preference.PreferenceManager; +import com.google.android.material.tabs.TabLayout; + import org.schabi.newpipe.R; import org.schabi.newpipe.database.subscription.NotificationMode; import org.schabi.newpipe.database.subscription.SubscriptionEntity; @@ -58,6 +60,7 @@ public class ChannelFragment extends BaseStateFragment { private Disposable subscriptionMonitor; private final CompositeDisposable disposables = new CompositeDisposable(); private SubscriptionManager subscriptionManager; + private int lastTab; private MenuItem menuRssButton; private MenuItem menuNotifyButton; @@ -94,10 +97,16 @@ public class ChannelFragment extends BaseStateFragment { public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + + if (savedInstanceState != null) { + lastTab = savedInstanceState.getInt("LastTab"); + } else { + lastTab = 0; + } } @Override - public void onAttach(@NonNull Context context) { + public void onAttach(final @NonNull Context context) { super.onAttach(context); subscriptionManager = new SubscriptionManager(activity); } @@ -119,6 +128,12 @@ public class ChannelFragment extends BaseStateFragment { binding.tabLayout.setupWithViewPager(binding.viewPager); } + @Override + public void onSaveInstanceState(final @NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putInt("LastTab", binding.tabLayout.getSelectedTabPosition()); + } + @Override public void onDestroy() { super.onDestroy(); @@ -284,8 +299,8 @@ public class ChannelFragment extends BaseStateFragment { } final String description = currentInfo.getDescription(); - if (description != null && !description.isEmpty() && - ChannelTabs.showChannelTab( + if (description != null && !description.isEmpty() + && ChannelTabs.showChannelTab( context, preferences, R.string.show_channel_tabs_info)) { tabAdapter.addFragment( ChannelInfoFragment.getInstance(currentInfo), "Info"); @@ -298,6 +313,12 @@ public class ChannelFragment extends BaseStateFragment { for (int i = 0; i < tabAdapter.getCount(); i++) { binding.tabLayout.getTabAt(i).setText(tabAdapter.getItemTitle(i)); } + + // Restore previously selected tab + final TabLayout.Tab ltab = binding.tabLayout.getTabAt(lastTab); + if (ltab != null) { + binding.tabLayout.selectTab(ltab); + } } @Override diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelInfoFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelInfoFragment.java index 6e7e49876..ba1faab8f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelInfoFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelInfoFragment.java @@ -79,7 +79,7 @@ public class ChannelInfoFragment extends BaseFragment { private void setupMetadata(final LayoutInflater inflater, final LinearLayout layout) { - Context context = getContext(); + final Context context = getContext(); if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) { addMetadataItem(inflater, layout, R.string.metadata_subscribers, diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java index 5a26371b7..1ce55df81 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java @@ -46,7 +46,7 @@ public class ChannelTabFragment extends BaseListInfoFragment