0

Currently, I am using an interface to communicate from fragment to activity. I want to pass updated model back to the activity on onDestroyView to update UI but that is causing crash when activity is destroyed by system or we enable "don't keep activity" then move activity to background. This causes crash as we use this context to fetch images and get exception You cannot start a load for a destroyed activity

I know we can use shared viewModel with liveData but that would seem overkill if we have single activity and multiple fragments.

we can also use setFragmentResult with supportFragmentManager.setFragmentResultListener but using this result would be available to all fragments attached to through this fragmentManager

can we use some thing else to make communication lifecycle aware?

Parth Gupta
  • 69
  • 1
  • 5
  • why use viewModel and liveData is overkill if we have single activity and multiple fragments? – KiluSs Aug 19 '22 at 08:56
  • if we have N fragments and single activity and want to communicate between them, then we can either have a single viewModel with N liveData or create N ViewModels (this would be better as it will have separation of concern) I think this is not an elegant solution – Parth Gupta Aug 23 '22 at 18:14

0 Answers0