java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout

9.5k Views Asked by At

When I try to insert Compose in overlay(draw over other apps) with XML I get this exception:

java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}

But without overlay(in activity) it works normal. Does anyone know how to resolve? I already updated AppCompat library to 1.3.0

My XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">
    <androidx.compose.ui.platform.ComposeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/compose_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>

My Overlay code:

mParams = WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT
)
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mView = layoutInflater.inflate(R.layout.power_overlay, null)
mParams!!.gravity = Gravity.CENTER
mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
mWindowManager.addView(mView, mParams)
5

There are 5 best solutions below

0
Transformer On
  1. Ensure that your constraint layout is updated to latest version in app level build.gradle file.

dependencies { ...    
 implementation 'androidx.constraintlayout:constraintlayout:1.3.x'
  1. To be sure, search & Replace ALL your xml tag names

<androidx.constraintlayout.ConstraintLayout>

 //with ->

<androidx.constraintlayout.widget.ConstraintLayout>

 

in every place CTRL + SHIFT + R _

  1. And inside gradle.properties add these:

 android.enableJetifier=true
 android.useAndroidX=true
  1. Clear out/Invalidate Caches and Restart Android Studio.

FileInvalidate Caches / RestartInvalidate and Restart

0
Mahozad On

For me it was because I had not included the appcompat library and my activity inherited from Activity instead of AppCompatActivity. The problem resolved by adding the library:

implementation("androidx.appcompat:appcompat:1.3.1")

and inheriting from AppCompatActivity:

class MyActivity: AppCompatActivity() {
  ...
}
0
AudioBubble On

For me, upgrade androidx.appcompat:appcompat from 1.0.0 to 1.4.1, the problem solved.

The fragment:

class Xxx : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val view = inflater.inflate(R.layout.fragment_xxx, container, false)

    view.findViewById<ComposeView>(R.id.compose_view).apply {
        setViewCompositionStrategy(
            ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner)
        )
        setContent {
            // compose
        }
    }
    return view
}

The xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
0
ali Hosseinnezhad On

I wrote this code and it works.

class AndroidComposeDialog<T>(
    private val activity: T,
    private val content: @Composable () -> Unit
) : Dialog(activity) where T : Context, T : androidx.lifecycle.LifecycleOwner, T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner {


    private fun initViewTreeOwners() {
        val window = window ?: return
        ViewTreeLifecycleOwner.set(window.decorView, activity)
        ViewTreeViewModelStoreOwner.set(window.decorView, activity)
        window.decorView.setViewTreeSavedStateRegistryOwner(activity)
        window.decorView.setViewTreeOnBackPressedDispatcherOwner(activity)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //here i init view tree owner that will be used by compose view
        initViewTreeOwners()
        //here i hide background of dialog
        window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT))
        val view = ComposeView(context).apply {
            setContent(content)
        }
        setContentView(view)
    }
}


// A function that returns an android dialog based on entered content
@Composable
fun androidDialog(content: @Composable () -> Unit): AndroidComposeDialog<*>? {
    val context = LocalContext.current
    val activity = context as? AppCompatActivity
    val componentActivity = context as? ComponentActivity
    val dialog = remember(activity, content.hashCode()) {
        if (activity != null)
            AndroidComposeDialog(activity = activity, content = content)
        else if (componentActivity != null)
            AndroidComposeDialog(activity = componentActivity, content = content)
        else null
    }
    return dialog
}

and now it's ready to use

@Composable
fun ShowDialog() {
    val dialog = androidDialog {
        Box(
            modifier = Modifier
                .size(100.dp)
                .clip(RoundedCornerShape(25.dp))
                .background(Color.Blue)
        )
    }
    Box(modifier = Modifier.fillMaxSize()) {
        Button(onClick = {
            dialog?.show()
        }, modifier = Modifier.align(Alignment.Center)) {
            Text(text = "show dialog")
        }
    }
}
0
Georgiy Chebotarev On

I was working with drawing overlay from foregound service and for me problem was here:

contentView = ComposeView(this).apply {
    setViewTreeSavedStateRegistryOwner(this@ForegroundService)
    setViewTreeLifecycleOwner(this@ForegroundService) // <-- missing line.
    setContent {
        OverlayComposeUi()
    }
}