Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist static member in Shared preferences when it is about to be Garbage collected in Android

I have a static member in Utility class, which is being accessed from most of my activities to store history info. I want to persist this static member when Utility class is about to be GCed. I tried below options.

  1. finalize() method implementation:

I have overriden finalize() of the Utility class(I know it is not always guaranteed that finalize() will run) to persist static member in Shared Preferences.But finalize() not called at all!

  1. Implementing onDestroy() in each activity to persist static member

I started implementing onDestroy() in all the activities which has access to this static member, when each activity is about to be destroyed, static member will be persisted in SharedPreference. This is working but writing to Shared preferences is happening very frequently causing unnecessary repetition of persists which I want to avoid.

Is there any better way to do this?

like image 282
Bhargav Kumar R Avatar asked Nov 28 '22 14:11

Bhargav Kumar R


1 Answers

Don't start implementing bad custom practices. Considering the documentation of onPause(), this is the place where you should persist the data.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)

You don't have any guarantee, that for any approach you'll reach the point of storing the data.

Considering that your Utility class only has static methods and members and you'd never create an instance of it, so finalize() is never called. If you're using instances of the Utility class, just persist the storage where you'd expect it to be last reachable.

Considering to use onDestroy() isn't a good idea either. The documentation states this explicitly.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

like image 183
tynn Avatar answered Feb 16 '23 00:02

tynn