Skip to content

[TabLayoutMediator] Don't try to detach an already detached TabLayout #4547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_IDLE;
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_SETTLING;

import android.util.Log;
import androidx.recyclerview.widget.RecyclerView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -44,6 +45,8 @@
* TabLayoutMediator.
*/
public final class TabLayoutMediator {
private static final String TAG = TabLayoutMediator.class.getSimpleName();

@NonNull private final TabLayout tabLayout;
@NonNull private final ViewPager2 viewPager;
private final boolean autoRefresh;
Expand Down Expand Up @@ -147,6 +150,11 @@ public void attach() {
* called before {@link #attach()} when a ViewPager2's adapter is changed.
*/
public void detach() {
if (!attached) {
Log.w(TAG, "TabLayoutMediator is already detached");
return;
}

if (autoRefresh && adapter != null) {
adapter.unregisterAdapterDataObserver(pagerAdapterObserver);
pagerAdapterObserver = null;
Expand Down