Activity/Fragment <-> AndroidViewModel <-> MyController
MyController will hold
- Executor
- MediaController
- Network access
- Database access
- Application context for above operation
Proper way to clear the resource
- Executor
// MyAndroidViewModel onClear() has been reached
if (myExecutor!=null) {
myExecutor.shutdown();
myExecutor = null;
} - Media Controller
if (myMediaController!=null) {
myMediaController.releaseMediaSession(); // MUST BE CLEAR
myMediaController = null;
} - Network access
myGetRadioLogo=null; - Database access
myDBAccess=null; - Application context
appContext=null; // MUST SET NULL AT ONCLEAR
Fail to release these resources may lead to memory leak
Fail to release resource with not properly sequence may lead application crashed, this crash can be found on logcat.