Exercise
Passing wrong (being too short-lived) Context
instances to ViewModels or repositories can lead to unexpected behavior during runtime. By using applicationContext
, ComposeUnitConverter makes sure this doesn’t happen. However, in production apps, you should try not to pass the Context
, but instead the object that uses the context. Let’s refactor ComposeUnitConverter so that the Repository
class constructor receives a SharedPreferences instance. Where do you need to put PreferenceManager.getDefaultSharedPreferences()
and what is important to keep in mind? Finally, which other code do you need to adapt once you have refactored the Repository
class?