Skip to content

Commit

Permalink
Revert to deprecated BEHAVIOR_SET_USER_VISIBLE_HINT in TabAdapter
Browse files Browse the repository at this point in the history
Also added comment explaining why
  • Loading branch information
Stypox committed Sep 6, 2020
1 parent 06d54ef commit 5b631e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
Expand All @@ -10,16 +11,20 @@
import java.util.ArrayList;
import java.util.List;

public class TabAdaptor extends FragmentPagerAdapter {
public class TabAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private final FragmentManager fragmentManager;

public TabAdaptor(final FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
public TabAdapter(final FragmentManager fm) {
// if changed to BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT => crash if enqueueing stream in
// the background and then clicking on it to open VideoDetailFragment:
// "Cannot setMaxLifecycle for Fragment not attached to FragmentManager"
super(fm, BEHAVIOR_SET_USER_VISIBLE_HINT);
this.fragmentManager = fm;
}

@NonNull
@Override
public Fragment getItem(final int position) {
return mFragmentList.get(position);
Expand Down Expand Up @@ -57,7 +62,7 @@ public void updateItem(final String title, final Fragment fragment) {
}

@Override
public int getItemPosition(final Object object) {
public int getItemPosition(@NonNull final Object object) {
if (mFragmentList.contains(object)) {
return mFragmentList.indexOf(object);
} else {
Expand All @@ -82,7 +87,9 @@ public void notifyDataSetUpdate() {
}

@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
public void destroyItem(@NonNull final ViewGroup container,
final int position,
@NonNull final Object object) {
fragmentManager.beginTransaction().remove((Fragment) object).commitNowAllowingStateLoss();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public class VideoDetailFragment

private AppBarLayout appBarLayout;
private ViewPager viewPager;
private TabAdaptor pageAdapter;
private TabAdapter pageAdapter;
private TabLayout tabLayout;
private FrameLayout relatedStreamsLayout;

Expand Down Expand Up @@ -752,7 +752,7 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {

appBarLayout = rootView.findViewById(R.id.appbarlayout);
viewPager = rootView.findViewById(R.id.viewpager);
pageAdapter = new TabAdaptor(getChildFragmentManager());
pageAdapter = new TabAdapter(getChildFragmentManager());
viewPager.setAdapter(pageAdapter);
tabLayout = rootView.findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
public class CommentsFragment extends BaseListInfoFragment<CommentsInfo> {
private CompositeDisposable disposables = new CompositeDisposable();

private boolean mIsVisibleToUser = false;

public static CommentsFragment getInstance(final int serviceId, final String url,
final String name) {
final CommentsFragment instance = new CommentsFragment();
Expand All @@ -39,12 +37,6 @@ public static CommentsFragment getInstance(final int serviceId, final String ur
// LifeCycle
//////////////////////////////////////////////////////////////////////////*/

@Override
public void setUserVisibleHint(final boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
mIsVisibleToUser = isVisibleToUser;
}

@Override
public void onAttach(final Context context) {
super.onAttach(context);
Expand Down Expand Up @@ -92,7 +84,7 @@ public void showLoading() {
public void handleResult(@NonNull final CommentsInfo result) {
super.handleResult(result);

AnimationUtils.slideUp(getView(), 120, 150, 0.06f);
AnimationUtils.slideUp(requireView(), 120, 150, 0.06f);

if (!result.getErrors().isEmpty()) {
showSnackBarError(result.getErrors(), UserAction.REQUESTED_COMMENTS,
Expand Down

0 comments on commit 5b631e0

Please sign in to comment.