Switching to particular component of Stack View

221 Views Asked by At

I'm trying to realize the button on the start page of Stack view which leads user to previously opened page. I did the minimum reproducible example below. Could you please help me why this solution doesn't work? I need to remember previously visited page (on each page there is a cancel button which leads user to the start page) and go back to that page by clicking the back butotn on the start page.

   Item {
            id: controller
            objectName: "controller"
    
            property var currentItem: page1Component
            property var previousItem: null
    
            onCurrentItemChanged: {
    
                var currentString = currentItem.toString()
                console.log ("currentString = " + currentString)
                if (currentItem !== null && previousItem != currentItem)
                {
                    stackView.pop()
                    stackView.push(currentItem)
                    previousItem = currentItem
                }
            }
    
    states: [
                State {
                    name: "testState"
    
                    PropertyChanges {
                        target: controller
                        currentItem: previousItem
                    }
                }    
      ]
            StackView {
                id: stackView
                anchors {
                    left: parent.left
                    right: parent.right
                    bottom: parent.bottom
                }
    
                Component {
                    id: page1Component
                    Page1 {   
                       onBackArrowClicked:
                        {
                            controller.state = "testState"
                        }                 
                    }
                }
    
                Component {
                    id: page2Component
                    Page2 {}
                }
    
0

There are 0 best solutions below