question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested ContourLayout causing StackOverflow

See original GitHub issue

The following code causes a stack overflow during layout:

    class ParentView(context: Context): ContourLayout(context) {
        init {
            setBackgroundColor(Color.RED)
        }

        val childView = ChildView(context).apply {
            applyLayout(
                x = leftTo { parent.left() }.widthOf { 100.xdip },
                y = topTo { parent.top() }.heightOf { 100.ydip }
            )
        }
    }

    class ChildView(context: Context) : ContourLayout(context) {
        init {
            setBackgroundColor(Color.BLUE)
        }
    }

However, moving ChildView’s applyLayout into it’s onInitializeLayout solves the issue:

    class ParentView(context: Context): ContourLayout(context) {
        init {
            setBackgroundColor(Color.RED)
        }

        val childView = ChildView(context).also { addView(it) }
    }

    class ChildView(context: Context) : ContourLayout(context) {
        init {
            setBackgroundColor(Color.BLUE)
        }

        override fun onInitializeLayout() {
            super.onInitializeLayout()
            applyLayout(
                x = leftTo { parent.left() }.widthOf { 100.xdip },
                y = topTo { parent.top() }.heightOf { 100.ydip }
            )
        }
    }

I don’t know if this is a bug or not, but my hunch is it’s the intended behavior (if not, I’d be happy to look into it).

I realize nesting layouts is discouraged, but there are going to be situations where it is useful and people are going to do it anyway.

The pattern Contour uses for configuring child views (MyView(context).apply { applyLayout() }) is great, and it seems clunky to have to violate it iff the child happens to be another ContourLayout (I can imagine during refactoring a View that didn’t start as Contour might become one later).

Could we make it so layout constraints declared at construction time get deferred until onInitializeLayout? Either by having applyLayout behave differently for ContourLayouts or with an onInitializeLayout DSL:

    class ParentView(context: Context): ContourLayout(context) {
        init {
            setBackgroundColor(Color.RED)
        }

        val childView = ChildView(context).apply {
            onInitializeLayout {
                applyLayout(
                    x = leftTo { parent.left() }.widthOf { 100.xdip },
                    y = topTo { parent.top() }.heightOf { 100.ydip }
                )
            }
        }
    }

    class ChildView(context: Context) : ContourLayout(context) {
        init {
            setBackgroundColor(Color.BLUE)
        }
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
saketcommented, Jun 8, 2020

I share your pain. We have a work-in-progress solution for this.

0reactions
dimsuzcommented, Jun 8, 2020

Getting caught by this over and over. (I keep forgetting about this issue). I use ContourLayout extensively in my new project, so naturally often child views are also CountourLayouts. Having to use .let instead of .apply makes code look irregular.

If possible I’d suggest to increase priority of this bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot find nested contours - opencv - Stack Overflow
I'm using findContours() and drawContours() method to find the contours in my binary image. However this is my output:.
Read more >
Stack overflow-free mapping over nested data - help
I would like to map over a deeply nested recursive data structure. Is there a safe way (i.e. no unwrap() etc.) to do...
Read more >
Newest 'hierarchical' Questions - Stack Overflow
I am trying to see how stable the hierarchical clustering is of return of stocks using different periods. I have the information of...
Read more >
Clustering 4000 Stack Overflow tags with BigQuery k-means
Let's find tags that usually go together: Co-occurring tags on Stack Overflow questions These groupings make sense: 'javascript' is related to ' ...
Read more >
3ds max: Maximum nested error and call stack overflow?
I hope someone can help me with this. I attempted to open my project this morning and 3dsMax gave me some weird error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found