IComponentCallbacks2 Interface

Definition

Callbacks for app state transitions that can be used to improve background memory management.

[Android.Runtime.Register("android/content/ComponentCallbacks2", "", "Android.Content.IComponentCallbacks2Invoker")]
public interface IComponentCallbacks2 : Android.Content.IComponentCallbacks, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("android/content/ComponentCallbacks2", "", "Android.Content.IComponentCallbacks2Invoker")>]
type IComponentCallbacks2 = interface
    interface IComponentCallbacks
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Derived
Attributes
Implements

Remarks

Callbacks for app state transitions that can be used to improve background memory management. This interface is available in all application components (android.app.Activity, android.app.Service, ContentProvider, and android.app.Application).

You should implement #onTrimMemory to release memory when your app goes to background states. Doing so helps the system keep your app's process cached in memory for longer, such that the next time that the user brings your app to the foreground, the app will perform a warm or hot start, resuming faster and retaining state.

<h2>Trim memory levels and how to handle them</h2>

<ul> <li>#TRIM_MEMORY_UI_HIDDEN<br> Your app's UI is no longer visible. This is a good time to release large memory allocations that are used only by your UI, such as android.graphics.Bitmap Bitmaps, or resources related to video playback or animations. <li>#TRIM_MEMORY_BACKGROUND<br> Your app's process is considered to be in the background, and has become eligible to be killed in order to free memory for other processes. Releasing more memory will prolong the time that your process can remain cached in memory. An effective strategy is to release resources that can be re-built when the user returns to your app. </ul>

Apps that continue to do work for the user when they're not visible can respond to the #TRIM_MEMORY_UI_HIDDEN callback by changing their behavior to favor lower memory usage. For example, a music player may keep full-sized album art for all tracks in the currently playing playlist as Bitmaps cached in memory. When the app is backgrounded but music playback continues, the app can change the caching behavior to cache fewer, or smaller, Bitmaps in memory.

The ordinal values for trim levels represent an escalating series of memory pressure events, with incrementing values accordingly. More states may be added in the future. As such, it's important not to check for specific values, but rather check if the level passed to #onTrimMemory is greater than or equal to the levels that your application handles. For example:

{@code
            public void onTrimMemory(int level) {
                if (level >= TRIM_MEMORY_BACKGROUND) {
                    // Release any resources that can be rebuilt
                    // quickly when the app returns to the foreground
                    releaseResources();
                } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
                    // Release UI-related resources
                    releaseUiResources();
                }
            }
            }

<strong>Note:</strong> the runtime may invoke Garbage Collection (GC) in response to application state changes. There is no need to explicitly invoke GC from your app.

Java documentation for android.content.ComponentCallbacks2.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

Name Description
Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniObjectReferenceControlBlock (Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Name Description
Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
OnConfigurationChanged(Configuration)

Called by the system when the device configuration changes while your component is running.

(Inherited from IComponentCallbacks)
OnLowMemory()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.

(Inherited from IComponentCallbacks)
OnTrimMemory(TrimMemory)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.

SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)

Extension Methods

Name Description
GetJniTypeName(IJavaPeerable)

Gets the JNI name of the type of the instance self.

JavaAs<TResult>(IJavaPeerable)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
TryJavaCast<TResult>(IJavaPeerable, TResult)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

Applies to