In Android, we can use Singleton
pattern to store data that we require across multiple activities during the whole app lifecycle. This is considered a memory leak and has its cons, but practically speaking, this approach is used widely (at least in Android programming).
Coming to the main point, in Navigation Component, we follow a single activity pattern which remains available throughout the app lifecycle. Also, this documentation says that a shared activity-level ViewModel
can be used to share data between different fragments. I think this is a fairly reasonable way to share data between multiple fragments but I still want to validate this approach before going ahead and using it with Navigation Component
. My question is whether we should use an activity-level ViewModel
to store small amounts of data that I need throughout the app lifecycle in a Navigation Component
based application (remember I only have a single activity)? Also, what implications/pros/cons this approach can have over the Singleton-based approach?