@@ -25,6 +25,7 @@ object SystemUiUtils {
2525 private const val THREE_BUTTON_NAV_BAR_OPACITY = 0.8f
2626
2727 private var statusBarBackgroundView: View ? = null
28+ private var statusBarBackgroundActivity: java.lang.ref.WeakReference <Activity >? = null
2829 private var navBarBackgroundView: View ? = null
2930 @JvmStatic
3031 var isEdgeToEdgeActive = false
@@ -88,27 +89,33 @@ object SystemUiUtils {
8889 sbView.setBackgroundColor(Color .BLACK )
8990 statusBarBackgroundView = sbView
9091 } else {
91- val contentLayout = activity.findViewById<ViewGroup >(android.R .id.content)
92- val view = View (activity).apply {
93- setBackgroundColor(Color .BLACK )
94- }
95- val params = FrameLayout .LayoutParams (
96- FrameLayout .LayoutParams .MATCH_PARENT , 0 , Gravity .TOP
97- )
98- contentLayout.addView(view, params)
99- statusBarBackgroundView = view
100-
101- ViewCompat .setOnApplyWindowInsetsListener(view) { v, insets ->
102- val sbHeight = insets.getInsets(WindowInsetsCompat .Type .statusBars()).top
103- val lp = v.layoutParams
104- if (lp.height != sbHeight) {
105- lp.height = sbHeight
106- v.layoutParams = lp
107- }
108- insets
92+ statusBarBackgroundActivity = java.lang.ref.WeakReference (activity)
93+ }
94+ }
95+
96+ private fun ensureStatusBarBackgroundView (): View ? {
97+ statusBarBackgroundView?.let { return it }
98+ val activity = statusBarBackgroundActivity?.get() ? : return null
99+ val contentLayout = activity.findViewById<ViewGroup >(android.R .id.content) ? : return null
100+ val view = View (activity)
101+ val params = FrameLayout .LayoutParams (
102+ FrameLayout .LayoutParams .MATCH_PARENT , 0 , Gravity .TOP
103+ )
104+ contentLayout.addView(view, params)
105+ statusBarBackgroundView = view
106+ statusBarBackgroundActivity = null
107+
108+ ViewCompat .setOnApplyWindowInsetsListener(view) { v, insets ->
109+ val sbHeight = insets.getInsets(WindowInsetsCompat .Type .statusBars()).top
110+ val lp = v.layoutParams
111+ if (lp.height != sbHeight) {
112+ lp.height = sbHeight
113+ v.layoutParams = lp
109114 }
110- view.requestApplyInsets()
115+ insets
111116 }
117+ view.requestApplyInsets()
118+ return view
112119 }
113120
114121 private fun setupNavigationBarBackground (contentLayout : ViewGroup ) {
@@ -169,6 +176,7 @@ object SystemUiUtils {
169176 @JvmStatic
170177 fun tearDown () {
171178 statusBarBackgroundView = null
179+ statusBarBackgroundActivity = null
172180 navBarBackgroundView = null
173181 isEdgeToEdgeActive = false
174182 isThreeButtonNav = false
@@ -217,15 +225,25 @@ object SystemUiUtils {
217225 Color .green(color),
218226 Color .blue(color)
219227 )
220- setStatusBarColor (window, opaqueColor)
228+ applyStatusBarColor (window, opaqueColor)
221229 }
222230
223231 /* *
224- * Sets the status bar background color.
225- * Uses the view-based background when available (edge-to-edge),
226- * falls back to the deprecated window API on older configurations .
232+ * Sets the status bar background color, lazily creating a manual view on API 35+
233+ * if the system view wasn't available at setup time. Use this for explicit app-level
234+ * color requests (e.g. from MainActivity) .
227235 */
228236 fun setStatusBarColor (window : Window ? , color : Int ) {
237+ val view = ensureStatusBarBackgroundView()
238+ if (view != null ) {
239+ view.setBackgroundColor(color)
240+ } else {
241+ @Suppress(" DEPRECATION" )
242+ window?.statusBarColor = color
243+ }
244+ }
245+
246+ private fun applyStatusBarColor (window : Window ? , color : Int ) {
229247 statusBarBackgroundView?.setBackgroundColor(color) ? : run {
230248 @Suppress(" DEPRECATION" )
231249 window?.statusBarColor = color
0 commit comments