In mobile applications, page jump is one of the problems that often needs to be dealt with. When using uniapp to develop cross-platform applications, the page jump mechanism is different from native development. One of the noteworthy differences is whether the current page is cleared after the page jumps.
In traditional native development, if we use startActivity or pushViewController and other methods to jump to the page, usually the current page will not be cleared, that is, the user can still see the previous page when returning. Page status. In uniapp, we usually use methods such as navigateTo or redirectTo to jump to pages. The navigateTo method will not clear the current page after jumping, but the redirectTo method will clear the current page.
Why do we need to clear the current page?
In the process of page jump, the behavior of clearing the current page may have a certain impact on the user experience, so why does such a design exist? We can discuss it from the following two aspects.
In native development, due to the existence of the virtual machine, the status of the current page is not directly stored in the memory, so when the page jumps There is no need to clear the current page. In uniapp, because the weex engine is used for rendering, the page status is stored directly in the application's memory. Therefore, if the current page is not cleared, additional memory resources will be consumed during the jump process. If the application has a large number of pages or a complex page state, the memory usage will increase accordingly, resulting in reduced application performance.
In uniapp, due to the cross-platform nature, users may use the application in different scenarios. For example, when users browse products on the mobile terminal, they may decide to complete the purchase on the PC terminal. At this time, the application needs to provide a similar experience on different platforms, that is, after the user completes the purchase, it will jump directly back to the product page and clear the status of the current page so that the user can search or browse again. In this scenario, clearing the current page can bring a better experience to the user.
How to deal with it in actual development?
In uniapp, we can jump to pages through different methods, so how to handle the status of the current page in actual development is also an important step. Usually we can handle it in the following two ways.
When using the navigateTo method to jump to a page, the status of the current page will not be cleared, that is, you can see the previous page when you jump back. state. If the application has a small number of pages or needs to maintain the status of the current page, you can choose to use the navigateTo method to jump. For example, the following code snippet:
uni.navigateTo({ url: '/pages/detail/detail?id=123' })
When using the redirectTo method to jump to a page, the status of the current page will be cleared, that is, when jumping back What you see is the reloaded page. If the application needs to clear the state of the current page and reload the page, it can choose to use the redirectTo method to jump. For example, the following code snippet:
uni.redirectTo({ url: '/pages/login/login' })
Of course, we can also freely handle the status of the current page when making a page jump, such as manually clearing the data of the current page before jumping or storing the status of the current page and Perform recovery operations when jumping back, etc.
Summary
Page jump is one of the commonly used functions in mobile applications, and it is also an essential part of uniapp cross-platform development. Although there are certain differences between uniapp and native development, developers only need to properly handle different jump methods to achieve the functions they need. When choosing a jump method, you need to make a selection based on the actual situation of the application, and process the status of the current page as needed.
The above is the detailed content of Why do you need to clear the current page when jumping to a page in uniapp?. For more information, please follow other related articles on the PHP Chinese website!