When my application starts, I download all the string data coming from the server to the hash map and all the screens read from this hash map to display button names, texts and screen names.
I hope that this object (and many other similar objects) will be saved for my application so that it remains functional.
I found that if I resume the application after a few hours, it still functions. But if I open another 10 applications, leave them open and resume the application after a few hours, he will try to read from one of these objects, but they are now gone!
In these cases, I believe that it is better to kill the application and restart it, and tried to do it in onDestroy of the main action, but it got the opposite reaction, because 5 seconds after switching to another activity, onDestroy of the previous action is called and the application will be killed using this code:
android.os.Process.killProcess(android.os.Process.myPid());
Therefore, I only want to kill the application when it was idle for several hours and the resources were canceled. Thus, when the user tries to resume it, he will die and start again, thereby again loading all the resources in memory.
I searched for the onDestroy method in the Application class, but does not exist. Thre - onTrimMemory method.
QUESTION:
How to use the Application.onTrimMemory () callback to destroy the resources of my application that it needs were destroyed?
EDIT: Actually, while reading the onTrimMemory documentation, Im left the impression that I should free it by destroying the objects, and if I do not, then how are the objects now null after resuming the application? I do not understand.
EDIT2:
I was able to partially solve my problem:
I downloaded the βFill Ramβ app from Google, made for developers who want to see how their app behaves in low memory situations.
I gradually began to fill the ram and saw how the onTrimMemory methods were called with values ββof 20, 40, 60, and 80, respectively
after every time I saw it being called in the logs, I quickly resumed my application, and then about 60 or 80, I needed zero resources:
I set the null pointer check to onResume, and if these resources were empty, I did the following:
if (application.data == null || application.data.size() == 0) { // Restart app so data is reloaded Log.e("kill app", "kill app"); Intent mStartActivity = new Intent(this,MainActivity.class); int mPendingIntentId = 123456; PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); System.exit(0); }
as indicated here:
how to programmatically restart the "Android app?
The problem remains half-resolved, because, in my opinion, in some other place of my application some other resource may be missing, so I'm considering the possibility of doing this in onTrimMemory 60 or 80?
I need more help understanding this.
Also whether to use
System.exit(0);
or
android.os.Process.killProcess(android.os.Process.myPid());
I did something else:
in my basic activity, that all other actions are expanding, I added the following:
@Override public void onTrimMemory(int level) { super.onTrimMemory(level); if (level > TRIM_MEMORY_MODERATE) {