2016-07-22 15 views
1

我實現了ComponentCallbacks2並且onTrimMemory永遠不會被調用。我正在嘗試使用Application類和我的自定義生命週期來管理內存。任何幫助,這是表示讚賞。來自ComponentCallbacks2的onTrimMemory()不會被調用

public class MemoryManager implements ComponentCallbacks2 { 
    private static List<MemoryInfo> memInfoList = new ArrayList<>(); 

    public interface MemoryInfo { 
     void releaseMemory(); 
    } 

    public static void registerMemoryListener(MemoryInfo memoryInfo) { 
     memInfoList.add(memoryInfo); 
    } 

    public static void unregisterMemoryListener(MemoryInfo memoryInfo) { 
     memInfoList.remove(memoryInfo); 
    } 

    @Override 
    public void onTrimMemory(int level) { 
     Log.i("TEST", "onTrimMemory called"); // does not get called 
     switch (level) { 
      case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: 
       try { 
        for (int i = memInfoList.size() - 1; i >= 0; i--) { 
         try { 
          memInfoList.get(i).releaseMemory(); // is this correct implementation? 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       break; 
      case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: 
       // I added logs here, it does not get reached 
       break; 
      default: 
       break; 
     } 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     // do nothing 
    } 

    @Override 
    public void onLowMemory() { 
     // will log when there is log memory 
    } 

我有一個應用程序類,從MemoryManager類調用我的接口。

public class TestApplication extends Application implements ActivityLifecycleCallback, MemoryManager.MemoryInfo { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Activity activity) { 
     Log.i(TAG, "onStart() called :: " + activity.getLocalClassName()); 
    } 

    @Override 
    public void onResume(Activity activity) { 
     Log.i(TAG, "onResume called :: " + activity.getLocalClassName()); 
     MemoryManager.registerMemoryListener(this); 
    } 

    @Override 
    public void onPause(Activity activity) { 
     Log.i(TAG, "onPause called :: " + activity.getLocalClassName()); 
    } 

    @Override 
    public void onStop(Activity activity) { 
     Log.i(TAG, "onStop called :: " + activity.getLocalClassName()); 
     MemoryManager.unregisterMemoryListener(this); 
    } 

    @Override 
    public void onDestroy(Activity activity) { 
     Log.i(TAG, "onDestroy called :: " + activity.getLocalClassName()); 
    } 

    @Override 
    public void releaseMemory() { 
     Log.i(TAG, "releaseMemory() called"); 
    } 

我的主要活動只是跟蹤生命週期。這工作正常。我的生命週期方法如下所示:

@Override 
protected void onDestroy() { 
    // register lifecycle 
    application.onDestroy(activity); 
    super.onDestroy(); 
} 

@Override 
protected void onPause() { 
    // register lifecycle 
    application.onPause(activity); 
    super.onPause(); 
} 

@Override 
protected void onResume() { 
    // register lifecycle 
    application.onResume(activity); 
    super.onResume(); 
} 

@Override 
protected void onStart() { 
    // register lifecycle 
    application.onStart(activity); 
    super.onStart(); 
} 

我缺少什麼來調用onTrimMemory()?

+1

AFAIK,'onTrimMemory'將只在必要時被調用;你應該檢查gt/lt'level',而不是eq;從一個活動調用應用程序生命週期方法也很奇怪。 – muratgu

回答

4

如果你看看the documentation for ComponentCallbacks2,你會注意到它已經在像ApplicationActivity這樣的類上實現。因此,對於這些組件,歡迎您僅覆蓋onTrimMemory(),並且會根據情況調用它。

這應該允許你從你的問題中刪除幾乎所有的代碼。

+0

我明白了。是的,這使事情變得更容易。謝謝! – portfoliobuilder

+1

@portfoliobuilder'Application'中的'onTrimMemory()'仍然沒有被調用。什麼爲你工作?來自'Activity'的那個確實受到了打擊 – Boy

2

你不應該實現ComponentCallbacks2,如果你想使用onTrimMemory(),你應該在應用程序類中覆蓋onTrimMemory()。但是,如果您確實想在自定義班級中正確使用,請使用registerComponentCallbacks(ComponentCallbacks callback)

如果您想開始跟蹤活動的生命週期,則可以使用registerActivityLifecycleCallbacks(callback);

如果回調是實現Application.ActivityLifecycleCallbacks

例如應用類,你可以在你的應用程序類的onCreate方法對其進行註冊。