简体   繁体   中英

Jetpack compose navigate forward

I have a simple navigation flow:

Screen A -> Screen B

After I do some work in Screen A, I navigate to Screen B, and do some work there. Then I remember I forgot to finish a task in Screen A, and I press the system back button and it takes me back to A. I can see all the local states are preserved/remembered (texts in textfield, counts, etc). As I finish my task, I want to return to Screen B, but it is already popped off of the stack. So I manually navigate to Screen B, and with no surprises, the local states are gone.

My goal is, as I can navigate backwards easily, I want to navigate forward in same manner (states preserved). Any help will be appreciated. Thanks.

Navigation isn't really designed to work like that, but you can work around it by manually saving the states yourself. You can save specific data in onSaveInstanceState() and restore it in onRestoreInstanceState() , however there are some cases when onRestoreInstanceState() isn't called when the activity is destroyed.

You're probably better of manually saving your state in SharedPreferences in onPause() , and restore them in onCreate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM