Added in API level 14

ComponentCallbacks2


interface ComponentCallbacks2 : ComponentCallbacks
Activity

An activity is a single, focused thing that the user can do.

Application

Base class for maintaining global application state.

ContentProvider

Content providers are one of the primary building blocks of Android applications, providing content to applications.

Fragment

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.

Service

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

VoiceInteractionSession

An active voice interaction session, providing a facility for the implementation to interact with the user in the voice interaction layer.

AbstractInputMethodService

AbstractInputMethodService provides a abstract base class for input methods.

AccessibilityService

Accessibility services should only be used to assist users with disabilities in using Android devices and apps.

AccountAuthenticatorActivity

Base class for implementing an Activity that is used to help implement an AbstractAccountAuthenticator.

ActivityGroup

A screen that contains and runs multiple embedded activities.

AliasActivity

Stub activity that launches another activity (and then finishes itself) based on information in its component's manifest meta-data.

AppFunctionService

Abstract base class to provide app functions to the system.

AutofillService

An AutofillService is a service used to automatically fill the contents of the screen on behalf of a given user - for more information about autofill, read Autofill Framework.

CallRedirectionService

This service can be implemented to interact between Telecom and its implementor for making outgoing call with optional redirection/cancellation purposes.

CallScreeningService

This service can be implemented by the default dialer (see TelecomManager.getDefaultDialerPackage()) or a third party app to allow or disallow incoming calls before they are shown to a user.

CameraPrewarmService

Extend this class to implement a camera prewarm service.

CarrierMessagingClientService

If the default SMS app has a service that extends this class, the system always tries to bind it so that the process is always running, which allows the app to have a persistent connection to the server.

CarrierMessagingService

A service that receives calls from the system when new SMS and MMS are sent or received.

CarrierService

A service that exposes carrier-specific functionality to the system.

ChooserTargetService

A service that receives calls from the system when the user is asked to choose a target for an intent explicitly by another app.

CloudMediaProvider

Base class for a cloud media provider.

CompanionDeviceService

A service that receives calls from the system with device events.

ConditionProviderService

A service that provides conditions about boolean state.

ConnectionService

An abstract service that should be implemented by any apps which either:

  1. Can make phone calls (VoIP or otherwise) and want those calls to be integrated into the built-in phone app. Referred to as a system managed ConnectionService.
  2. Are a standalone calling app and don't want their calls to be integrated into the built-in phone app. Referred to as a self managed ConnectionService.
Once implemented, the needs to take the following steps so that Telecom will bind to it:

ControlsProviderService

Service implementation allowing applications to contribute controls to the System UI.

CredentialProviderService

Service to be extended by credential providers, in order to return user credentials to the framework.

DeviceAdminService

Base class for a service that device owner/profile owners can optionally have.

DialogFragment

A fragment that displays a dialog window, floating on top of its activity's window.

DocumentsProvider

Base class for a document provider.

DreamService

Extend this class to implement a custom dream (available to the user as a "Daydream").

ExpandableListActivity

An activity that displays an expandable list of items by binding to a data source implementing the ExpandableListAdapter, and exposes event handlers when the user selects an item.

HostApduService

HostApduService is a convenience Service class that can be extended to emulate an NFC card inside an Android service component.

HostNfcFService

HostNfcFService is a convenience Service class that can be extended to emulate an NFC-F card inside an Android service component.

InCallService

This service is implemented by an app that wishes to provide functionality for managing phone calls.

InputMethodService

InputMethodService provides a standard implementation of an InputMethod, which final implementations can derive from and customize.

IntentService

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intents) on demand.

IsolatedService

Base class for services that are started by ODP on a call to OnDevicePersonalizationManager#execute(ComponentName, PersistableBundle, java.util.concurrent.Executor, OutcomeReceiver) and run in an isolated process.

JobService

Entry point for the callback from the android.app.job.JobScheduler.

LauncherActivity

Displays a list of all activities which can be performed for a given intent.

ListActivity

An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

ListFragment

A fragment that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

MediaBrowserService

Base class for media browser services.

MediaRoute2ProviderService

Base class for media route provider services.

MediaSession2Service

This API is not generally intended for third party application developers.

MidiDeviceService

A service that implements a virtual MIDI device.

MidiUmpDeviceService

A service that implements a virtual MIDI device for Universal MIDI Packets (UMP).

MockApplication

A mock android.app.Application class.

MockContentProvider

Mock implementation of ContentProvider.

NativeActivity

Convenience for implementing an activity that will be implemented purely in native code.

NotificationListenerService

A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

OffHostApduService

OffHostApduService is a convenience Service class that can be extended to describe one or more NFC applications that are residing off-host, for example on an embedded secure element or a UICC.

PreferenceActivity

This is the base class for an activity to show a hierarchy of preferences to the user.

PreferenceFragment

Shows a hierarchy of Preference objects as lists.

PrintService

This is the base class for implementing print services.

QuickAccessWalletService

A QuickAccessWalletService provides a list of WalletCards shown in the Quick Access Wallet.

RecognitionService

This class provides a base class for recognition service implementations.

RemoteViewsService

The service to be connected to for a remote adapter to request RemoteViews.

SearchRecentSuggestionsProvider

This superclass can be used to create a simple search suggestions provider for your application.

SettingInjectorService

Dynamically specifies the summary (subtitle) and enabled status of a preference injected into the list of app settings displayed by the system settings app

SettingsPreferenceService

Base class for a service that exposes its settings preferences to external access.

SliceProvider

A SliceProvider allows an app to provide content to be displayed in system spaces.

SpellCheckerService

SpellCheckerService provides an abstract base class for a spell checker.

TabActivity

For apps developing against android.os.Build.VERSION_CODES#HONEYCOMB or later, tabs are typically presented in the UI using the new ActionBar.newTab() and related APIs for placing tabs within their action bar area.

TextToSpeechService

Abstract base class for TTS engine implementations.

TileService

A TileService provides the user a tile that can be added to Quick Settings.

TvAdService

The TvAdService class represents a TV client-side advertisement service.

TvInputService

The TvInputService class represents a TV input or source such as HDMI or built-in tuner which provides pass-through video or broadcast TV programs.

TvInteractiveAppService

A TV interactive application service is a service that provides runtime environment and runs TV interactive applications.

VisualVoicemailService

This service is implemented by dialer apps that wishes to handle OMTP or similar visual voicemails.

VoiceInteractionService

Top-level service of the current global voice interactor, which is providing support for hotwording, the back-end of a android.app.VoiceInteractor, etc.

VoiceInteractionSessionService

An active voice interaction session, initiated by a VoiceInteractionService.

VpnService

VpnService is a base class for applications to extend and build their own VPN solutions.

VrListenerService

A service that is bound from the system while running in virtual reality (VR) mode.

WallpaperService

A wallpaper service is responsible for showing a live wallpaper behind applications that would like to sit on top of it.

WebViewFragment

A fragment that displays a WebView.

Extended ComponentCallbacks interface with a new callback for finer-grained 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 incrementally release memory based on current system constraints. Using this callback to release your resources helps provide a more responsive system overall, but also directly benefits the user experience for your app by allowing the system to keep your process alive longer. That is, if you don't trim your resources based on memory levels defined by this callback, the system is more likely to kill your process while it is cached in the least-recently used (LRU) list, thus requiring your app to restart and restore all state when the user returns to it.

The values provided by onTrimMemory do not represent a single linear progression of memory limits, but provide you different types of clues about memory availability:

  • When your app is running:
    1. TRIM_MEMORY_RUNNING_MODERATE
      The device is beginning to run low on memory. Your app is running and not killable.
    2. TRIM_MEMORY_RUNNING_LOW
      The device is running much lower on memory. Your app is running and not killable, but please release unused resources to improve system performance (which directly impacts your app's performance).
    3. TRIM_MEMORY_RUNNING_CRITICAL
      The device is running extremely low on memory. Your app is not yet considered a killable process, but the system will begin killing background processes if apps do not release resources, so you should release non-critical resources now to prevent performance degradation.
  • When your app's visibility changes:
    1. TRIM_MEMORY_UI_HIDDEN
      Your app's UI is no longer visible, so this is a good time to release large resources that are used only by your UI.
  • When your app's process resides in the background LRU list:
    1. TRIM_MEMORY_BACKGROUND
      The system is running low on memory and your process is near the beginning of the LRU list. Although your app process is not at a high risk of being killed, the system may already be killing processes in the LRU list, so you should release resources that are easy to recover so your process will remain in the list and resume quickly when the user returns to your app.
    2. TRIM_MEMORY_MODERATE
      The system is running low on memory and your process is near the middle of the LRU list. If the system becomes further constrained for memory, there's a chance your process will be killed.
    3. TRIM_MEMORY_COMPLETE
      The system is running low on memory and your process is one of the first to be killed if the system does not recover memory now. You should release absolutely everything that's not critical to resuming your app state.

      To support API levels lower than 14, you can use the onLowMemory method as a fallback that's roughly equivalent to the ComponentCallbacks2.TRIM_MEMORY_COMPLETE level.

    Note: When the system begins killing processes in the LRU list, although it primarily works bottom-up, it does give some consideration to which processes are consuming more memory and will thus provide more gains in memory if killed. So the less memory you consume while in the LRU list overall, the better your chances are to remain in the list and be able to quickly resume.

More information about the different stages of a process lifecycle (such as what it means to be placed in the background LRU list) is provided in the Processes and Threads document.

Summary

Constants
static Int

Level for onTrimMemory(int): the process has gone on to the LRU list.

static Int

Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.

static Int

Level for onTrimMemory(int): the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running low on memory.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running moderately low on memory.

static Int

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so.

Public methods
abstract Unit
onTrimMemory(level: Int)

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

Inherited functions
Unit onConfigurationChanged(newConfig: Configuration)

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.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

For more information, read Handling Runtime Changes.

Unit onLowMemory()

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.

Constants

TRIM_MEMORY_BACKGROUND

Added in API level 14
static val TRIM_MEMORY_BACKGROUND: Int

Level for onTrimMemory(int): the process has gone on to the LRU list. This is a good opportunity to clean up resources that can efficiently and quickly be re-built if the user returns to the app.

Value: 40

TRIM_MEMORY_COMPLETE

Added in API level 14
Deprecated in API level 35
static val TRIM_MEMORY_COMPLETE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.

Value: 80

TRIM_MEMORY_MODERATE

Added in API level 14
Deprecated in API level 35
static val TRIM_MEMORY_MODERATE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.

Value: 60

TRIM_MEMORY_RUNNING_CRITICAL

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_CRITICAL: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running. Your running process should free up as many non-critical resources as it can to allow that memory to be used elsewhere. The next thing that will happen after this is onLowMemory() called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user.

Value: 15

TRIM_MEMORY_RUNNING_LOW

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_LOW: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running low on memory. Your running process should free up unneeded resources to allow that memory to be used elsewhere.

Value: 10

TRIM_MEMORY_RUNNING_MODERATE

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_MODERATE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running moderately low on memory. Your running process may want to release some unneeded resources for use elsewhere.

Value: 5

TRIM_MEMORY_UI_HIDDEN

Added in API level 14
static val TRIM_MEMORY_UI_HIDDEN: Int

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so. Large allocations with the UI should be released at this point to allow memory to be better managed.

Value: 20

Public methods

onTrimMemory

Added in API level 14
abstract fun onTrimMemory(level: Int): Unit

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. 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.

To retrieve the processes current trim level at any point, you can use ActivityManager.getMyMemoryState(RunningAppProcessInfo).