Reputation: 325
I have application that has MainActivity, that starts InvoiceActivity and that starts InvoicePaymentActivity which finally starts PaymentSuccessActivity.
I started using architecture components and it seems to work fine, bud I have found problem when starting MainActivity from PaymentSuccessActivity.
Up until now, I would just start new Intent and the app would "reset" to main screen. With ViewModel I am getting "Cannot add the same observer with different lifecycles".
I have found 2 solutions, but cannot think which one is better:
Subscribe to observer in onResume, unsubscribe onPause
Finish all previous activities except MainActivity after the next one is started. So when I just finish PaymentSuccessActivity, user will be on MainActivity. This has a drawback of user navigating backwards...
But it seems I cannot add the observer again... how can I unsibscribe/subscribe? My code does not work right now...
override fun onResume() {
super.onResume()
viewModel.intercom.observe(this, observer)
}
override fun onPause() {
super.onPause()
viewModel.intercom.removeObserver { observer }
}
Upvotes: 3
Views: 3833
Reputation: 325
So this whole problem was base on using anonymous Observer class. Once I created my observer class implementing observer interface, the app startet to work fine, without needing to manually observer/remove. Can anyone explain why this is issue?
Upvotes: 1