-
-
Notifications
You must be signed in to change notification settings - Fork 714
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
New viewmodel for every instance of same fragment #145
Comments
Hello, you are injecting with You have to use |
I am sorry. This one is working (it is using the android standard implementation: class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ViewModelProviders.of(this).get(HomeViewModel::class.java).liveData.observe(this,
Observer<List<Items>> {
})
} And this one is not working (using di with koin) class HomeFragment : Fragment() {
private val homeViewModel: HomeViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
homeViewModel.liveData.observe(this,
Observer<List<Items>> {
})
} Steps to reproduce:
|
Ok, what project did you use? |
Yes i am using koin-android-architecture v0.9.3 |
If you can make a very small project that reproduces your case (just a public GitHub project), I would be very nice and help us save time 👍 |
Of course... :-) You have to use Android Studio 3.2 Canary 18 When you checkout the repo it is working like intended.
If you change HomeFragment to this the app crashes after clicking back on second fragment:
class HomeFragment : Fragment() {
// not working
private val homeViewModel: HomeViewModel2 by viewModel()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// working
//val homeViewModel = ViewModelProviders.of(this).get(HomeViewModel2::class.java)
homeViewModel.cryptocurrencyLiveData.observe(this,
Observer<List<Cryptocurrency>> {
})
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
detailsButton.setOnClickListener {
view.findNavController().navigate(R.id.navToDetails)
}
}
} |
Thanks! |
You have to declare a ViewModel class with val homeModule: Module = applicationContext{
context("home") {
bean { CryptocurrencyRepository(get(), get()) }
viewModel { HomeViewModel(get()) }
viewModel { HomeViewModel2() }
}
} I've added a protection to not allow injection of definition that are not ViewModel 👍 ( |
I'll push you a PR to fix your sample. |
Shame on me... I looked everywhere if i use it the correct way but did not see it... SORRY! |
No problem :) This was also the moment to reinforce viewmodel injection check ;) |
@arnaudgiuliani I'm using version I'm also using Navigation Component to navigate between fragments. When I navigate from fragment A to B, and then navigate back to A, a new ViewModel is created and the observer is called multiple times. Is there something that I'm doing wrong? |
Please double check your ViewModel injection in activity or fragment, should be "by viewModel()" or "by sharedViewModel()", not "by inject()". |
@arnaudgiuliani same problem here also I'm using version |
Please reopen a new ticket |
@joelfernandes19, I had the same issue. When i rotate screen, my activity in onCreate method created a new instance of fragment. When we create a new instance of fragment, it takes a new instance of ViewModel. I just do like that |
Describe the bug
I have a fragment which has a personId to show details of a person
To Reproduce
Steps to reproduce the behavior:
Expected behavior
If i open a new instance of the same fragment it should also create a new viewmodel.
Koin project used and used version (please complete the following information):
koin-android-architecture v0.9.3
The text was updated successfully, but these errors were encountered: