0

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 {}
                }
    
Valeriia
  • 586
  • 2
  • 4
  • 21
  • The StackView does the remembering for you, you can simply use `push` when entering a new page and `pop` when pressing cancel on that page – Amfasis Apr 11 '22 at 07:28
  • Isn't it what I'm doing in onCurrentItemChanged ? – – Valeriia Apr 11 '22 at 09:43
  • You push and pop in the same function, not on the separate events. I think you want to use `initialItem` and use push whenever the user goes into a cancellable page, and use pop whenever the cancel is pressed (which then leads to the initialItem) – Amfasis Apr 11 '22 at 10:40
  • I tried this way as well(pushing whenever user goes to a cancellable page and popping on cancel clicked). But the main concern is that I don't know during compilation which exactly page I need to push clicking onBackArrowClicked (see above), it depends on the previous page. – Valeriia Apr 11 '22 at 11:57
  • What is more, I need to get the index of this previous page – Valeriia Apr 11 '22 at 12:17
  • ok, then I'm not sure I understand correctly. Anyway, it is possible to add properties to a page, during the push operation, possibly this can help your mission – Amfasis Apr 11 '22 at 14:09

0 Answers0