Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewModel is not retained #49

Closed
leangl opened this issue Oct 28, 2017 · 1 comment
Closed

ViewModel is not retained #49

leangl opened this issue Oct 28, 2017 · 1 comment

Comments

@leangl
Copy link

leangl commented Oct 28, 2017

After screen rotation a new ViewModel is always provided for fragments. Moving the creation of the model from onAttach to onCreate solves the problem.

@mboukadir
Copy link

mboukadir commented Nov 26, 2017

I had the same problem and I am very interested to know the reason why After screen rotation a new ViewModel is always provided for fragment when we create ViewModel in onAttach.

I tried to dig in my side, but I could not find the answer what I noticed is that:
The ViewModels are stored in a ViewModelStore, which is held by HolderFragment.
This HolderFragment is marked as retain a fragment so he must survive to the configuration change and onDestroy() will not be called (but onDetach () still will be, because the fragment is being detached from its current activity).

But in our case onDestroy is called and the ViewModelStore is cleared . as you can see in HolderFragment#onDestory method

@Override
    public void onDestroy() {
        super.onDestroy();
        mViewModelStore.clear();
    }

If my analysis is good the real question is why HolderFragment#onDestroy is called after the configuration change even if he is marked as retain ?

Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants