From 657125f43ca96cf0785be74b0867ab83d4b8a106 Mon Sep 17 00:00:00 2001 From: yausername <13ritvik@gmail.com> Date: Sun, 24 Mar 2019 06:31:28 +0530 Subject: [PATCH] save selected tab sate in stream detail fragment, fixes #2238 --- .../schabi/newpipe/fragments/detail/TabAdaptor.java | 13 +++++++++++++ .../fragments/detail/VideoDetailFragment.java | 13 +++++++++++++ app/src/main/res/values/settings_keys.xml | 1 + 3 files changed, 27 insertions(+) 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 27cc3ec8a..8314f9539 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 @@ -1,5 +1,6 @@ package org.schabi.newpipe.fragments.detail; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; @@ -61,6 +62,18 @@ public class TabAdaptor extends FragmentPagerAdapter { else return POSITION_NONE; } + public int getItemPositionByTitle(String title) { + return mFragmentTitleList.indexOf(title); + } + + @Nullable + public String getItemTitle(int position) { + if (position < 0 || position >= mFragmentTitleList.size()) { + return null; + } + return mFragmentTitleList.get(position); + } + public void notifyDataSetUpdate(){ notifyDataSetChanged(); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 8c87c1875..68d1b49ea 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -122,6 +122,7 @@ public class VideoDetailFragment private boolean autoPlayEnabled; private boolean showRelatedStreams; private boolean showComments; + private String selectedTabTag; @State protected int serviceId = Constants.NO_SERVICE_ID; @@ -213,6 +214,9 @@ public class VideoDetailFragment showComments = PreferenceManager.getDefaultSharedPreferences(activity) .getBoolean(getString(R.string.show_comments_key), true); + selectedTabTag = PreferenceManager.getDefaultSharedPreferences(activity) + .getString(getString(R.string.stream_info_selected_tab_key), COMMENTS_TAB_TAG); + PreferenceManager.getDefaultSharedPreferences(activity) .registerOnSharedPreferenceChangeListener(this); } @@ -226,6 +230,10 @@ public class VideoDetailFragment public void onPause() { super.onPause(); if (currentWorker != null) currentWorker.dispose(); + PreferenceManager.getDefaultSharedPreferences(getContext()) + .edit() + .putString(getString(R.string.stream_info_selected_tab_key), pageAdapter.getItemTitle(viewPager.getCurrentItem())) + .apply(); } @Override @@ -815,6 +823,9 @@ public class VideoDetailFragment } private void initTabs() { + if (pageAdapter.getCount() != 0) { + selectedTabTag = pageAdapter.getItemTitle(viewPager.getCurrentItem()); + } pageAdapter.clearAllItems(); if(shouldShowComments()){ @@ -835,6 +846,8 @@ public class VideoDetailFragment if(pageAdapter.getCount() < 2){ tabLayout.setVisibility(View.GONE); }else{ + int position = pageAdapter.getItemPositionByTitle(selectedTabTag); + if(position != -1) viewPager.setCurrentItem(position); tabLayout.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index b6c214caf..3861f53d5 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -139,6 +139,7 @@ show_play_with_kodi show_next_video show_comments + stream_info_selected_tab show_hold_to_append en GB