Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.37k stars 75 forks source link

"Complex" update of activity title - e.g. load an item and update activity title #249

Closed MFlisar closed 3 years ago

MFlisar commented 3 years ago

In your examples you show how to set the activity title based on the screen in an easy way, meaning that the screen always knows its title.

Consider that the screen holds an id of a database item, the fragment loads this item and AFTERWARDS it can update the activities title. How would you design this?

My current solution now is that all screens set the activity title to my apps name and then my fragments can savely update the title when they've loaded their data and on screen change the next screen will reset the title again. But wonder if there is way that you consider is more "simple stack" like.

Zhuinden commented 3 years ago

Hey, in this scenario what I'd normally do is retrieve the title before navigating to the new screen, pass it to the key as title but use it as the initial value of a BehaviorRelay/MutableLiveData (whichever you use) in a scoped service (model), and use Bundleable to persist/restore that field's value. Then I observe that and set that as title. You can even make internal subscription in the scoped service to observe the data to ensure that the title is in sync with the underlying DB (either onServiceRegistered/onServiceUnregistered or onServiceActive/onServiceInactive would work).

Normally this is easier btw when toolbar title is managed at screen level, not activity level, but it can work either way. The key point here (heh) is that the key would contain only initial value for me, and I'd save/restore latest correct value in scoped model.

MFlisar commented 3 years ago

Thanks for taking the time to explain this, got the idea behind it - this helped me and answered my question.