Giter VIP home page Giter VIP logo

Comments (10)

MikaReesu avatar MikaReesu commented on June 22, 2024 3

Thanks a lot for your help!

It is working perfectly !

from simple-stack.

Zhuinden avatar Zhuinden commented on June 22, 2024

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.

MikaReesu avatar MikaReesu commented on June 22, 2024

from simple-stack.

Zhuinden avatar Zhuinden commented on June 22, 2024

...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.

MikaReesu avatar MikaReesu commented on June 22, 2024

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.

Zhuinden avatar Zhuinden commented on June 22, 2024

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.

MikaReesu avatar MikaReesu commented on June 22, 2024

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.

Zhuinden avatar Zhuinden commented on June 22, 2024

private val key: HomeScreen should work

from simple-stack.

MikaReesu avatar MikaReesu commented on June 22, 2024

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.

Zhuinden avatar Zhuinden commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.