Application的onLowMemory从Android API 34开始系统不再触发,从API 35开始废弃

Application的onLowMemory从Android API 34开始系统不再触发,从API 35开始废弃

Android的Application的onLowMemory()是在ComponentCallbacks定义实现,源代码链接 https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/ComponentCallbacks.java

java 复制代码
package android.content;

import android.annotation.NonNull;
import android.content.res.Configuration;

/**
 * The set of callback APIs that are common to all application components
 * ({@link android.app.Activity}, {@link android.app.Service},
 * {@link ContentProvider}, and {@link android.app.Application}).
 *
 * <p class="note"><strong>Note:</strong> You should also implement the {@link
 * ComponentCallbacks2} interface, which provides the {@link
 * ComponentCallbacks2#onTrimMemory} callback to help your app manage its memory usage more
 * effectively.</p>
 */
public interface ComponentCallbacks {
    /**
     * Called by the system when the device configuration changes while your
     * component is running.  Note that, unlike activities, other components
     * are never restarted when a configuration changes: they must always deal
     * with the results of the change, such as by re-retrieving resources.
     *
     * <p>At the time that this function has been called, your Resources
     * object will have been updated to return resource values matching the
     * new configuration.
     *
     * <p>For more information, read <a href="{@docRoot}guide/topics/resources/runtime-changes.html"
     * >Handling Runtime Changes</a>.
     *
     * @param newConfig The new device configuration.
     */
    void onConfigurationChanged(@NonNull Configuration newConfig);

    /**
     * This is called when the overall system is running low on memory, and
     * actively running processes should trim their memory usage.  While
     * the exact point at which this will be called is not defined, generally
     * it will happen when 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.
     *
     * @deprecated Since API level 14 this is superseded by
     *             {@link ComponentCallbacks2#onTrimMemory}.
     *             Since API level 34 this is never called.
     *             Apps targeting API level 34 and above may provide an empty implementation.
     */
    @Deprecated
    void onLowMemory();
}

关于onLowMemory(),Android系统源代码这么说:

* @deprecated Since API level 14 this is superseded by

* {@link ComponentCallbacks2#onTrimMemory}.

* Since API level 34 this is never called.

* Apps targeting API level 34 and above may provide an empty

从 API 34开始,该方法过时了,不再被触发回调。

Android官方链接说明:

https://developer.android.com/reference/android/content/ComponentCallbacks#onLowMemory()

从API 35开始,废弃。此后这里是一个空实现。

Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值-CSDN博客文章浏览阅读751次,点赞8次,收藏8次。Android的ComponentCallbacks2接口定义了内存警戒水位线值,用于在系统内存不足时通知应用释放资源。从API 34开始,仅保留TRIM_MEMORY_BACKGROUND(40)和TRIM_MEMORY_UI_HIDDEN(20)两个有效值,分别表示进程进入后台LRU列表和UI不再可见时应释放资源。开发者不应直接比较值大小,而应判断是否大于等于某个级别。如Glide框架会根据不同级别释放不同量的内存资源。该机制能有效帮助系统管理内存,延长应用进程存活时间。https://blog.csdn.net/zhangphil/article/details/149119633Android之Application的onTerminate能监听应用退出吗?-CSDN博客文章浏览阅读3w次,点赞8次,收藏9次。 Android之Application的onTerminate能监听应用退出吗?一些Android开发者在不经意间发现Android的Application中有一个公开的回调方法:onTerminate()继而想当然的认为该方法即是Android的整个App应用退出后的回调,因为Terminate的词面意思就是..._onterminatehttps://blog.csdn.net/zhangphil/article/details/81232302