Comments (10)
Thanks a lot for your help!
It is working perfectly !
from simple-stack.
Heyo,
Keys are passed to Fragments as arguments (via setArguments(Bundle().apply { putParcelable(DefaultFragmentArgs.ARGS_KEY, key))
), and fragment arguments don't change over time once they are already set.
Also, by default, the getFragmentTag()
returns getClass().getName();
as the "unique identifier" of a fragment, but if you are okay with destroying the previously existing HomeFragment
and its scope, and replace it with a new scope, you can do return toString();
, in which case the Fragment will be completely replaced with a new instance with the new arguments set.
Otherwise, normally I keep such selection state in a BehaviorRelay/MutableLiveData/MutableStateFlow etc and only use the key as the initial parameter for it. That way I can make changes to the selected state, keep it across process death, but don't try to override the initial arguments.
I hope that helps think of a solution, currently what happens in your case is "expected" (because of how fragment arguments are intended to work + what the default value of the fragment tag is) .
from simple-stack.
from simple-stack.
...that moment when you've been using this for ages but you don't have an open-source example 🤔
Anyhoo, the idea is that HomeViewModel
can be passed the key
as a constructor parameter, and in HomeViewModel
you could do
private val selectedIndex = MutableLiveData(key.selectedIndex)
Once you have this, then you can persist the selected index via Bundleable
class HomeViewModel(...): Bundleable {
...
override fun toBundle(): StateBundle = StateBundle().apply {
putInt("selectedIndex", selectedIndex.value)
}
override fun fromBundle(bundle: StateBundle?) {
bundle?.run {
selectedIndex = getInt("selectedIndex", 0)
}
}
}
Once you have that, you can update the value in HomeViewModel easily
private val homeViewModel by lazy { lookup<HomeViewModel>() }
...
homeViewModel.updateSelectedIndex(newIndex)
And then you don't have to use the key to do it and you keep the ViewModel/Fragment instead of destroying them.
Not sure if overhead, I'm kinda used to it as this is how Fragment arguments worked in general as far as I'm aware 🤔 or at least in case of Activities, you were definitely not able to re-define the extras and actually preserve this change, which is why DefaultFragmentStateChanger
doesn't try to invoke fragment.setArguments(
on each state change, but only on creation.
Please notify if this solves your issue
from simple-stack.
That's interesting, I think it will be much cleaner to use this method.
I still have a few questions related to that.
private val selectedIndex = MutableLiveData(key.selectedIndex)
Where is the "key.selectedIndex" coming from?
So, the value is passing by the bundle of the ViewModel and not by the HomeScreen if I understand well.
And, finally the last piece of code will be used in the child Fragment to update the data before doing the goTo right?
private val homeViewModel by lazy { lookup<HomeViewModel>() }
homeViewModel.updateSelectedIndex(newIndex)
Thank you for your help and the time you take to answer.
from simple-stack.
Where is the "key.selectedIndex" coming from?
That code is assuming that you keep it as initial param, but serviceBinder
which creates the homeViewModel
in add(HomeViewModel(lookup(), backstack, lookup()))
actually has a getKey<T>
function (and ``this@HomeScreen` is also the key to begin with) so the VM can be passed arguments during initialization.
So, the value is passing by the bundle of the ViewModel and not by the HomeScreen if I understand well.
That's how I generally do it, because I use args for initialization, but the rest happens via the "ViewModels" (scoped services).
And, finally the last piece of code will be used in the child Fragment to update the data before doing the goTo right?
Yes 👍
from simple-stack.
So I need to add the getKey to HomeScreen :
add(HomeViewModel(**getKey()**, lookup(), backstack, lookup()))
In the HomeViewModel, Do I need to declare a key or no? The following code has errors, but is it suppose to be similar to this?
class HomeViewModel(
private val key: <T>,
private val backstack: Backstack,
) : ScopedServices.Registered, Bundleable {
private val selectedIndex = MutableLiveData(key.indexPage)
Sorry, to ask again, but I didn't think that I understood all correctly.
Thank you, again!
from simple-stack.
private val key: HomeScreen
should work
from simple-stack.
Oh I see, yeah working with the HomeScreen.
In the HomeViewModel I created the function updateindexPage.
Is it gonna update it or I need to call toBundle in it?
:
fun updateindexPage(index: Int){
indexPage.value = index
}
In the child fragment, is it correct?
homeViewModel.updateindexPage(2)
backstack.goTo(HomeScreen( 2, true))
So, after in the HomeFragment, to get the Value, I can"t do anymore, no?
val (indexPage, isRedirected) = getKey<HomeScreen>()
Do I need to pass by HomeViewModel to get the data here ?
from simple-stack.
In the HomeViewModel I created the function updateindexPage.
Is it gonna update it
It should, because of how LiveData
works
or I need to call toBundle in it?
toBundle()
is called by simple-stack, it's basically just like onSaveInstanceState
.
So, after in the HomeFragment, to get the Value, I can"t do anymore, no?
Well, you should get the selected index from your ViewModel liveData with .observe
now.
from simple-stack.
Related Issues (20)
- Allow removing a `Backstack.CompletionListener` from inside the listener itself HOT 5
- Clarification: can a service be registered/unregistered more than once? HOT 5
- Can I Use @styles/Theme.MyComposeTheme on AndroidManifests.xml? HOT 3
- Question: What is the purpose of RegistrationViewModel inside ServiceProvider in extension-compose-example? HOT 4
- What is the proper way to add a key to the top even if it already exists in the backstack? HOT 2
- Publish to MavenCentral instead of just Jitpack.io
- Unable to start activity ComponentInfo{com.xxxxxxxxxx.yyyyyy/com.xxxxxxxxxx.yyyyyy.activities.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment q2.v1: could not find Fragment constructor HOT 7
- How to 'lookup<>()' scoped class from an Android Service HOT 2
- Lazy initialization of ScopedServices HOT 2
- KMM support? HOT 2
- Screen can get the service from wrong scope during back animation HOT 6
- Backstack is not available in a child Fragment's onCreate after process death
- DialogFragment and BottomDialogFragment as part of navigation stack, question HOT 2
- Verify that the new `setParentServices()`'s `lookupFromScope(..., EXPLICIT)` behavior is what is expected
- Updating the Compose example to Compose 1.5.0 requires AGP 8.1
- Should Simple Stack be used in new projects HOT 14
- Crash due to activity.onBackPressed() when removing fragment hosting a WebView in AndroidView {} + FADE HOT 15
- Backstack with espresso HOT 3
- The service does not exist in any accessible scopes HOT 11
- Up navigation examples are hard to find HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-stack.