I have a text field in a screen that is part of a ...
# compose
n
I have a text field in a screen that is part of a navigation graph accessed through a bottom nav. When I type something in the text field, and switch to another tab, and then back again, the text field is empty. I was expecting the state to be saved, but if I need to save this text in a ViewModel or something, that's fine. Any ideas what's happening here?
i
Are you using
rememberSaveable
for your text? You have to save the text to restore the text when you come back to the screen
n
I am, yes.
And, my bottom navigation implementation is by the book, straight out of the documentation including the saveState and restoreState = true.
c
I thought when navigating through bottom nav... by default the composable is disposed? but if you put the state in a VM the VM would stick around?
n
That could be, for sure. Part of why I asked. Nothing I try seems to get the text to stick on this field.
i
rememberSaveable
survives disposal and recreation - that's the saveable part
n
I tried rewriting this a bit to put the state into the ViewModel, but honestly, this is just state related to the text field, sort of internal stuff that really shouldn't need to be put into the VM. But I'll see if I can get it working this way for now.
c
@Neal Sanche if you potentially have a minimal repro project that I can git clone, I can potentially check it out if you want another set of eyes.
n
Thanks @Colton Idle. I laugh a bit, because nobody has minimal repro projects just sitting around. 🙂 Though maybe I'll make a quickie and see if it has the same issues. Just hold on a sec. 🙂
i
One easy thing to test is if navigating to another destination (just adding it to the back stack via the default
navigate()
) and hitting the back button keeps your text saved. If so, then your problem isn't with how you are saving state, but probably something with your bottom nav
(i.e., using login as your start destination)
n
Sure, will try that now.
So, if I do what you suggested, @Ian Lake by modifying my bottom nav to just do a navigate without all of the popUpTo and launchSingleTop and restoreState, navigate to another item, then simply hit the back button on the device (which I assume just pops the backstack) the text seems to remain in the search field. Interesting.
i
So it does sound like how you've set up your bottom nav or your graph is the problem
👍 1
n
Maybe I'm just expecting something different when tapping the bottom nav icons than what it actually does. When I navigate to another tab (the search field is in the first tab, the home tab) and hit the back button, I go back and the search field is still filled. If I tap another tab, and then tap the home tab, the search field is empty again. Probably because in the second case, it recreates the composable?
i
Both recreate the composable. The state will be the same in both cases though
that's assuming you aren't misusing the start destination for login, splash screens, etc. That start destination should always be the base of your back stack
Is your home tab your start destination?
n
Yes it is.
I made a minimal repro repo, @Colton Idle https://github.com/nealsanche/NavTextField
i
Do you see the same problem if you use alpha04?
n
Just tried it, and in fact, I don't see the same issue.
Tried the same downgrade on my actual project, and it worked there too. Will leave it in your capable hands, Ian. Thanks for the attention tonight, but I'm going to stop for now.
i
Do you mind adding that sample project to a new issue against Navigation? https://issuetracker.google.com/issues/new?component=409828 I'd like to confirm it is fixed for alpha06 or get the fix in 🙂
c
Nice find @Neal Sanche!
n
I don't mind, Ian, will do.