Implementation example
public class AppContext extends Application {
public class ActivityParent extends Activity implements AppContext.IMemoryInfo { protected ActivityParent child; @Override protected void onStop() { super.onStop(); try { if (child != null) AppContext.unregisterMemoryListener(child); } catch (Exception e) { } } }
public class ActivityChild extends ActivityParent { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); child = this; } /---move following onResume() in parent as following eg: @Override protected void onResume() { super.onResume(); AppContext.registerMemoryListener(this); } @Override public void goodTimeToReleaseMemory() { super.goodTimeToReleaseMemory();
Additional Information:
When your application is running: TRIM_MEMORY_RUNNING_MODERATE The device starts to run low in memory. Your application works and does not kill.
TRIM_MEMORY_RUNNING_LOW The device is operating significantly lower in memory. Your application works and does not kill, but please free up unused resources to improve system performance (which directly affects the performance of your application).
TRIM_MEMORY_RUNNING_CRITICAL The device runs very low in memory. Your application is not yet considered a killer process, but the system will begin to kill background processes if the applications do not release resources, so now you must free up non-critical resources to prevent performance degradation.
When the visibility of your application changes: TRIM_MEMORY_UI_HIDDEN The user interface of the application is no longer displayed, so now is the best time to release large resources that are used only by your user interface.
When your application process is in the LRU background list: TRIM_MEMORY_BACKGROUND The system runs on low memory, and your process is close to the top of the LRU list. Although your application process is not at high risk of being killed, the system may already kill processes on the LRU list, so you must free resources that are easy to recover, so your process will remain on the list and resume quickly when the user returns to your application.
TRIM_MEMORY_MODERATE The system runs on low memory, and your process is in the middle of the LRU list. If the system is even more limited for memory, there is a chance that your process will be killed.
TRIM_MEMORY_COMPLETE The system runs on low memory, and your process is one of the first to be killed if the system does not recover memory. You must release absolutely everything that is not critical for the resumption of the state of your application. To keep API levels below 14, you can use the onLowMemory() method as a reserve, which is roughly equivalent to the TRIM_MEMORY_COMPLETE level.
http://developer.android.com/reference/android/content/ComponentCallbacks2.html