Giter VIP home page Giter VIP logo

Comments (1)

NikitaGvozdilko avatar NikitaGvozdilko commented on May 28, 2024

The problem is in ViewAttachmentManager. It adds FrameLayout to WindowManager and non of them have reference on LifecycleOwner.

Here is workaround I created temporary. Replace lib's ViewAttachmentManager on this one:

`import android.content.Context
import android.graphics.PixelFormat
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeViewModelStoreOwner
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.google.ar.sceneform.rendering.ViewAttachmentManager

/**

  • Updated implementation of ViewAttachmentManager with access to frameLayout

  • Don't forget to add to lifecycle observers otherwise View will not be rendered
    */
    class ComposableViewAttachmentManager(
    context: Context,
    private val lifecycleOwner: LifecycleOwner,
    private val viewModelStoreOwner: ViewModelStoreOwner,
    private val savedStateRegistryOwner: SavedStateRegistryOwner,
    private val ownerView: View // Used to post callbacks onto the UI thread.
    ): ViewAttachmentManager(context, ownerView), DefaultLifecycleObserver {
    private val windowManager: WindowManager
    private val windowLayoutParams: WindowManager.LayoutParams
    private val frameLayout: FrameLayout
    private val viewLayoutParams: ViewGroup.LayoutParams

    init {
    windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowLayoutParams = createWindowLayoutParams()
    frameLayout = FrameLayout(context)
    viewLayoutParams = createViewLayoutParams()

     frameLayout.setViewTreeLifecycleOwner(lifecycleOwner)
     frameLayout.setViewTreeSavedStateRegistryOwner(savedStateRegistryOwner)
     frameLayout.setViewTreeViewModelStoreOwner(viewModelStoreOwner)
    

    }

    override fun onResume(owner: LifecycleOwner) {
    onResume()
    }

    override fun onPause(owner: LifecycleOwner) {
    onPause()
    }

    override fun onResume() {
    // A ownerView can only be added to the WindowManager after the activity has finished resuming.
    // Therefore, we must use post to ensure that the window is only added after resume is finished.
    ownerView.post {
    if (frameLayout.parent == null && ownerView.isAttachedToWindow) {
    windowManager.addView(frameLayout, windowLayoutParams)
    }
    }
    }

    override fun onPause() {
    // The ownerView must be removed from the WindowManager before the activity is destroyed, or the
    // window will be leaked. Therefore we add/remove the ownerView in resume/pause.
    if (frameLayout.parent != null) {
    windowManager.removeView(frameLayout)
    }
    }

    /**

    • Add a ownerView as a child of the [FrameLayout] that is attached to the [ ].
    • Used by [RenderViewToExternalTexture] to ensure that the ownerView is drawn with all
    • appropriate lifecycle events being called correctly.
      */
      override fun addView(view: View) {
      if (view.parent === frameLayout) {
      return
      }
      frameLayout.addView(view, viewLayoutParams)
      }

    /**

    • Remove a ownerView from the [FrameLayout] that is attached to the [WindowManager].
    • Used by [RenderViewToExternalTexture] to remove ownerView's that no longer need to be
    • drawn.
      */
      override fun removeView(view: View) {
      if (view.parent !== frameLayout) {
      return
      }
      frameLayout.removeView(view)
      }

    companion object {
    private const val VIEW_RENDERABLE_WINDOW = "ViewRenderableWindow"
    private fun createWindowLayoutParams(): WindowManager.LayoutParams {
    val params = WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
    or WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    PixelFormat.TRANSLUCENT
    )
    params.title = VIEW_RENDERABLE_WINDOW
    return params
    }

     private fun createViewLayoutParams(): ViewGroup.LayoutParams {
         return ViewGroup.LayoutParams(
             ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
         )
     }
    

    }
    }
    `

from sceneview-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.