Open
Description
- 2.0.3
- All devices / Android 12, Poco X3 Pro
I'm trying to display a notification with a blur on top of all views. When a notification is shown, Compose stops updating, does not respond to clicks.
I wrote a small sample where this can be reproduced:
https://github.com/zakrodionov/ComposeBugStuckComposition
NotificationView.kt
class NotificationView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
private val view: View = View.inflate(context, R.layout.view_notification, this)
private val blurView = view.findViewById<BlurView>(R.id.blurView)
init {
val decorView = getActivityDecorView()
val rootView = decorView?.findViewById(android.R.id.content) as ViewGroup
val windowBackground = decorView.background
blurView.setupWith(rootView, RenderScriptBlur(context)) // or RenderEffectBlur
.setFrameClearDrawable(windowBackground)
.setBlurRadius(2f)
}
private fun getActivityDecorView(): View? {
var ctx = context
var i = 0
while (i < 4 && ctx != null && ctx !is Activity && ctx is ContextWrapper) {
ctx = ctx.baseContext
i++
}
return if (ctx is Activity) {
ctx.window.decorView
} else {
null
}
}
}
ComposeFragment.kt
private fun showNotification() {
val viewGroup =
requireActivity().window?.decorView?.findViewById(android.R.id.content) as? ViewGroup
val view =
NotificationView(requireContext()).apply {
setOnClickListener { viewGroup?.removeView(it) }
}
val params =
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
params.gravity = Gravity.TOP or Gravity.CENTER
viewGroup?.addView(view, params)
view.bringToFront()
}
view_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical">
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/purple_200"
app:blurOverlayColor="#A353536C">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Notification title"
android:textSize="30sp" />
</eightbitlab.com.blurview.BlurView>
</LinearLayout>
Metadata
Metadata
Assignees
Labels
No labels