question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android screen rotation creates new instance of fragment which causes the instance of viewmodel to be lost

See original GitHub issue

Describe the bug I am using - implementation "org.koin:koin-androidx-viewmodel:2.1.0-alpha-3"

In my modules, I declare it as - private val viewModels = module { viewModel { (handle: SavedStateHandle) -> HomeViewModel(handle, get()) } }

I create my HomeFragment inside onCreate() of MainActivity when savedInstanceState == null.

In my HomeFragment I inject my viewModel like this -

private val homeViewModel: HomeViewModel by viewModel { parametersOf( Bundle(), "homeViewModel" ) }

And in HomeFragment, inside onViewCreated() I observe the changes in livedata -

homeViewModel.showResults().observe(this, Observer { .... })

When the screen is rotated to landscape, the onCreate() of MainActivity is called again and the fragment is newly created and the viewModel instance in the fragment is gone.

Is it not possible for the viewmodel instance to be retained even after screen rotation change?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
songzhwcommented, Nov 8, 2021

I tried and found out two interesting things:

if you are using val vm: MyViewModel by inject(), the viewModel can’t survive config change.

But if you use:

import org.koin.androidx.viewmodel.ext.android.viewModel

class MyActivity : AppCompatActivity() {
    val vm: MyViewModel by viewModel()

Now the viewModel could survive config change

1reaction
wax911commented, May 9, 2020

I used to have this issue with a similar approach, I had a couple of fragments and a navigation drawer and each time I had a configuration change when the views are reconstructed my fragments would somehow get a new instance of their viewmodel/s, which didn’t seem right, considering this lifecycle diagram, but I guess this is true for activities but not fragments?

image

What I had to do was to use tags/ids in my supportFragmentManager when I commit and upon getting navigation item change I would call supportFragmentManager.findFragmentByTag and do a null check on the result, if it is null I’ll create a new instance of my fragment.

Additionally I also use retainInstance on my fragments, the difference in lifecycle handling are as follows: Complete lifecycle

image

Hope this helps you 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - ViewModel Fragment Recreates On Screen Rotation
Every time it recreates my fragment. I don't want to save or retain any views using methods. Because ViewModel used to protect view...
Read more >
Using ViewModel to Fix Screen Rotation Issues - YouTube
In this video, we walk through an example of adding a ViewModel in our Android app. ViewModels are a great way to ensure...
Read more >
Fragment lifecycle - Android Developers
The fragment's view Lifecycle is created only when your Fragment provides a valid View instance. In most cases, you can use the fragment ......
Read more >
The Curious Case of Survival of Android ViewModel - Medium
ViewModel is a separate class which has an instance of it in the activity and has no reference for view in it. So...
Read more >
How ViewModels survive configuration changes
Usually, one of the first things we find out when learning Android development is that activities get re-created after configuration changes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found