Detect whether application is quit by the OS because of low RAM

感情迁移 提交于 2019-12-23 16:23:47

问题


In the application I'm building, I need to detect the application quitting if and only if the application has been quit when its in the background because the OS is reclaiming memory.

From my own experimentation, the onDestroy is called on EVERY instance. I've tried checking for isFinishing but I'm not 100% sure which situations this isolates it to.

@Override
public void onDestroy()
{
    super.onDestroy();
    Log.i("V LIFECYCLE", "onDestroy");
    if (!isFinishing())
    {
        // are we here because the OS shut it down because of low memory?
        ApplicationPreferences pref = new ApplicationPreferences(this);
        // set persistant flag so we know next time that the user
        // initiated the kill either by a direct kill or device restart.
        pref.setThePersistantFlag(true);
        Log.i("DEBUG", "onDestroy - ensuring that the next launch will result in a log out..");
    }
}

Can anyone shed light on my issue here? Thankyou.


回答1:


Through trial and error I have worked out a solution that works perfectly for anyone thats interested. I have narrowed down the case when the application state is being resumed (onResume) in the case of the OS reclaiming memory.

public boolean wasJustCollectedByTheOS = false; 

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    // this flag will only be present as long as the task isn't physically killed
    // and/or the phone is not restarted.
    savedInstanceState.putLong("semiPersistantFlag", 2L);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    long semiPersistantFlag = savedInstanceState.getLong("semiPersistantFlag");
    if (semiPersistantFlag == 2L)
    {
        savedInstanceState.putLong("semiPersistantFlag", 0L);
        this.wasJustCollectedByTheOS = true;   
    }
}

// this gets called immediately after onRestoreInstanceState
@Override
public void onResume() {
    if (this.wasJustCollectedByTheOS){
        this.wasJustCollectedByTheOS = false;
        // here is the case when the resume is after an OS memory collection    
    }
}



回答2:


I don't know whether its help you or not,

From Android Activity class,

public void onLowMemory ()

This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. While the exact point at which this will be called is not defined, generally it will happen around the time all background process have been killed, that is before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

Applications that want to be nice can implement this method to release any caches or other unnecessary resources they may be holding on to. The system will perform a gc for you after returning from this method.

And Since: API Level 14

public abstract void onTrimMemory (int level)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. This will happen for example when it goes in the background and there is not enough memory to keep as many background processes running as desired. You should never compare to exact values of the level, since new intermediate values may be added -- you will typically want to compare if the value is greater or equal to a level you are interested in.



来源:https://stackoverflow.com/questions/11624842/detect-whether-application-is-quit-by-the-os-because-of-low-ram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!