Set SurfaceView lifecycle to follow attachment in PlayerView

This avoids destroying the surface if PlayerView is hidden in the
view hierarchy.

PiperOrigin-RevId: 568501459
This commit is contained in:
tonihei 2023-09-26 04:54:30 -07:00 committed by Copybara-Service
parent 212f1f8ea8
commit 69ffac28bb

View File

@ -49,6 +49,7 @@ import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DoNotInline;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@ -427,7 +428,11 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
}
break;
default:
surfaceView = new SurfaceView(context);
SurfaceView view = new SurfaceView(context);
if (Util.SDK_INT >= 34) {
Api34.setSurfaceLifecycleToFollowsAttachment(view);
}
surfaceView = view;
break;
}
surfaceView.setLayoutParams(params);
@ -1737,4 +1742,13 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
}
}
}
@RequiresApi(34)
private static class Api34 {
@DoNotInline
public static void setSurfaceLifecycleToFollowsAttachment(SurfaceView surfaceView) {
surfaceView.setSurfaceLifecycle(SurfaceView.SURFACE_LIFECYCLE_FOLLOWS_ATTACHMENT);
}
}
}