Skip to content

Commit d6501c9

Browse files
committed
[Slider] Fix attach/detach behavior when Slider is added in an overlay as part of a Transition.
Resolves material-components#1195 A Visibility transition will add Slider to an overlay and then call onAttachToWindow/onDetachFromWindow which looks for an android.R.id.content view, which doen't exist, getting stuck in a loop. This adds an exit strategy for android.R.id.content not being found and fixes Slider in Visibility transitions. PiperOrigin-RevId: 305886706
1 parent df84e91 commit d6501c9

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/java/com/google/android/material/internal/ViewUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ public static float getParentAbsoluteElevation(@NonNull View view) {
271271
* version from androidx when it's available.
272272
*/
273273
@Nullable
274-
public static ViewOverlayImpl getOverlay(@NonNull View view) {
274+
public static ViewOverlayImpl getOverlay(@Nullable View view) {
275+
if (view == null) {
276+
return null;
277+
}
275278
if (Build.VERSION.SDK_INT >= 18) {
276279
return new ViewOverlayApi18(view);
277280
}
@@ -288,6 +291,10 @@ public static ViewGroup getContentView(@Nullable View view) {
288291
}
289292
if (parent.getParent() instanceof ViewGroup) {
290293
parent = (ViewGroup) parent.getParent();
294+
} else if (parent.getParent() == null) {
295+
// If android.R.id.content has not been found and parent has no more parents to search,
296+
// exit.
297+
return null;
291298
}
292299
}
293300
return null;

lib/java/com/google/android/material/slider/Slider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.google.android.material.drawable.DrawableUtils;
6868
import com.google.android.material.internal.DescendantOffsetUtils;
6969
import com.google.android.material.internal.ThemeEnforcement;
70+
import com.google.android.material.internal.ViewOverlayImpl;
7071
import com.google.android.material.internal.ViewUtils;
7172
import com.google.android.material.resources.MaterialResources;
7273
import com.google.android.material.shape.CornerFamily;
@@ -1291,8 +1292,11 @@ protected void onDetachedFromWindow() {
12911292
}
12921293

12931294
for (TooltipDrawable label : labels) {
1294-
ViewUtils.getContentViewOverlay(this).remove(label);
1295-
label.detachView(ViewUtils.getContentView(this));
1295+
ViewOverlayImpl contentViewOverlay = ViewUtils.getContentViewOverlay(this);
1296+
if (contentViewOverlay != null) {
1297+
contentViewOverlay.remove(label);
1298+
label.detachView(ViewUtils.getContentView(this));
1299+
}
12961300
}
12971301

12981302
super.onDetachedFromWindow();

lib/java/com/google/android/material/tooltip/TooltipDrawable.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,10 @@ public void setLayoutMargin(@Px int layoutMargin) {
339339
*
340340
* @see #detachView(View)
341341
*/
342-
public void setRelativeToView(@NonNull View view) {
342+
public void setRelativeToView(@Nullable View view) {
343+
if (view == null) {
344+
return;
345+
}
343346
updateLocationOnScreen(view);
344347
// Listen for changes that indicate the view has moved so the location can be updated
345348
view.addOnLayoutChangeListener(attachedViewLayoutChangeListener);
@@ -350,7 +353,10 @@ public void setRelativeToView(@NonNull View view) {
350353
*
351354
* @see #setRelativeToView(View)
352355
*/
353-
public void detachView(@NonNull View view) {
356+
public void detachView(@Nullable View view) {
357+
if (view == null) {
358+
return;
359+
}
354360
view.removeOnLayoutChangeListener(attachedViewLayoutChangeListener);
355361
}
356362

0 commit comments

Comments
 (0)