在我正在構建的應用程序中,當且僅當應用程序在後臺退出時,我需要檢測應用程序退出,因爲操作系統正在回收內存。檢測應用程序是否由於低RAM而被操作系統退出
從我自己的實驗中,在每個實例上調用onDestroy。我試過檢查isFinishing,但我並不是100%確定這種情況將其隔離。
@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..");
}
}
任何人都可以在這裏闡明我的問題嗎?謝謝。
它沒有爲我工作。我只是旋轉我的手機,上面的代碼告訴說,這是因爲操作系統內存收集。 – Ali 2017-07-16 17:39:17