I am currently building an app that will have a passcode to enter at launch. I am storing this passcode value in datastore as using the room API for just one value seems silly. Thinking ahead, I know two activities will need to access passcode information: the launching activity and the settings activity to change the passcode. The problem with this is I see no way of accessing the datastore again after creation. I would like advice on how to proceed with accessing this information in multiple places.
I do have some ideas on how to get around this problem however as there does not seem to be a getDataStore function in existance.
Create a viewModel to contain passcode information so that datastore can act like a singleton class.(Problem: viewModels can not be shared across activities as answered here)
Create a singleton class that contains a dataStore instance in a companion object. (Problem: An instance of this singleton class created in activity is not guaranteed to maintain companion object values, once initial instance is destroyed as answered [here] (Do companion objects remain in memory for app's lifecycle), this will be a problem when accessing later on in settings after the launching activity is destroyed with finish()) Perhaps the solution to this problem is to pass an instance of the class in a bundle to the next activity, but I doubt a dataStore object is serializable to pass via bundle.
Multi-Process Data Store. This one admittedly boggles my mind on how to achieve. I know it runs CRUD operations via a service. I feel this is the most valid approach to my problem but I am confused by the official google guide on how to code my own version. The official google documentation is here
- The main problem with the above solution is my unfamiliarity with coding services. I am also unsure if multiple activities count as multiple process code, I possibly think so as two activities have their own lifecycles and hence processes?
You can use SharedPreferences to store data and access anywhere from ur app instead of Room DB.
Here is a rough dummy code -
Store this FUNCTION_TIME_KEY key in some constants file or somewhere
you can even create a common class where you can set and get all your shared pref values just by passing Keys. Hope this Helps