Skip to content

Most visited

Recently visited

navigation

ContextCompat

public class ContextCompat
extends Object

java.lang.Object
   ↳ android.support.v4.content.ContextCompat
Known Direct Subclasses
Known Indirect Subclasses


Helper for accessing features in Context introduced after API level 4 in a backwards compatible fashion.

Summary

Public constructors

ContextCompat()

Public methods

static int checkSelfPermission(Context context, String permission)

Determine whether you have been granted a particular permission.

static Context createDeviceProtectedStorageContext(Context context)

Return a new Context object for the current Context but whose storage APIs are backed by device-protected storage.

static File getCodeCacheDir(Context context)

Returns the absolute path to the application specific cache directory on the filesystem designed for storing cached code.

static final int getColor(Context context, int id)

Returns a color associated with a particular resource ID

Starting in M, the returned color will be styled for the specified Context's theme.

static final ColorStateList getColorStateList(Context context, int id)

Returns a color state list associated with a particular resource ID.

static File getDataDir(Context context)

Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored.

static final Drawable getDrawable(Context context, int id)

Returns a drawable object associated with a particular resource ID.

static File[] getExternalCacheDirs(Context context)

Returns absolute paths to application-specific directories on all external storage devices where the application can place cache files it owns.

static File[] getExternalFilesDirs(Context context, String type)

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns.

static final File getNoBackupFilesDir(Context context)

Returns the absolute path to the directory on the filesystem similar to getFilesDir().

static File[] getObbDirs(Context context)

Returns absolute paths to application-specific directories on all external storage devices where the application's OBB files (if there are any) can be found.

static boolean isDeviceProtectedStorage(Context context)

Indicates if the storage APIs of this Context are backed by device-encrypted storage.

static boolean startActivities(Context context, Intent[] intents, Bundle options)

Start a set of activities as a synthesized task stack, if able.

static boolean startActivities(Context context, Intent[] intents)

Start a set of activities as a synthesized task stack, if able.

Inherited methods

From class java.lang.Object

Public constructors

ContextCompat

ContextCompat ()

Public methods

checkSelfPermission

int checkSelfPermission (Context context, 
                String permission)

Determine whether you have been granted a particular permission.

Parameters
context Context
permission String: The name of the permission being checked.
Returns
int PERMISSION_GRANTED if you have the permission, or PERMISSION_DENIED if not.

See also:

createDeviceProtectedStorageContext

Context createDeviceProtectedStorageContext (Context context)

Return a new Context object for the current Context but whose storage APIs are backed by device-protected storage.

On devices with direct boot, data stored in this location is encrypted with a key tied to the physical device, and it can be accessed immediately after the device has booted successfully, both before and after the user has authenticated with their credentials (such as a lock pattern or PIN).

Because device-protected data is available without user authentication, you should carefully limit the data you store using this Context. For example, storing sensitive authentication tokens or passwords in the device-protected area is strongly discouraged.

If the underlying device does not have the ability to store device-protected and credential-protected data using different keys, then both storage areas will become available at the same time. They remain as two distinct storage locations on disk, and only the window of availability changes.

Each call to this method returns a new instance of a Context object; Context objects are not shared, however common state (ClassLoader, other Resources for the same configuration) may be so the Context itself can be fairly lightweight.

Prior to isAtLeastN() this method returns null, since device-protected storage is not available.

Parameters
context Context
Returns
Context

See also:

getCodeCacheDir

File getCodeCacheDir (Context context)

Returns the absolute path to the application specific cache directory on the filesystem designed for storing cached code. On devices running LOLLIPOP or later, the system will delete any files stored in this location both when your specific application is upgraded, and when the entire platform is upgraded.

This location is optimal for storing compiled or optimized code generated by your application at runtime.

Apps require no extra permissions to read or write to the returned path, since this path lives in their private storage.

Parameters
context Context
Returns
File The path of the directory holding application code cache files.

getColor

int getColor (Context context, 
                int id)

Returns a color associated with a particular resource ID

Starting in M, the returned color will be styled for the specified Context's theme.

Parameters
context Context
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int A single color value in the form 0xAARRGGBB.
Throws
Resources.NotFoundException if the given ID does not exist.

getColorStateList

ColorStateList getColorStateList (Context context, 
                int id)

Returns a color state list associated with a particular resource ID.

Starting in M, the returned color state list will be styled for the specified Context's theme.

Parameters
context Context
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
ColorStateList A color state list, or null if the resource could not be resolved.
Throws
Resources.NotFoundException if the given ID does not exist.

getDataDir

File getDataDir (Context context)

Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored. Apps should not use this path directly; they should instead use getFilesDir(), getCacheDir(), getDir(String, int), or other storage APIs on Context.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

No additional permissions are required for the calling app to read or write files under the returned path.

Parameters
context Context
Returns
File

See also:

getDrawable

Drawable getDrawable (Context context, 
                int id)

Returns a drawable object associated with a particular resource ID.

Starting in LOLLIPOP, the returned drawable will be styled for the specified Context's theme.

Parameters
context Context
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
Drawable Drawable An object that can be used to draw this resource.

getExternalCacheDirs

File[] getExternalCacheDirs (Context context)

Returns absolute paths to application-specific directories on all external storage devices where the application can place cache files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like getCacheDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

  • External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
  • There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by StatFs.

Starting in KITKAT, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, WRITE_EXTERNAL_STORAGE is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using android:maxSdkVersion like this:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />

The first path returned is the same as getExternalCacheDir(). Returned paths may be null if a storage device is unavailable.

Parameters
context Context
Returns
File[]

See also:

getExternalFilesDirs

File[] getExternalFilesDirs (Context context, 
                String type)

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

  • External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
  • There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by StatFs.

Starting in KITKAT, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, WRITE_EXTERNAL_STORAGE is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using android:maxSdkVersion like this:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />

The first path returned is the same as getExternalFilesDir(String). Returned paths may be null if a storage device is unavailable.

Parameters
context Context
type String
Returns
File[]

See also:

getNoBackupFilesDir

File getNoBackupFilesDir (Context context)

Returns the absolute path to the directory on the filesystem similar to getFilesDir(). The difference is that files placed under this directory will be excluded from automatic backup to remote storage on devices running LOLLIPOP or later. See BackupAgent for a full discussion of the automatic backup mechanism in Android.

No permissions are required to read or write to the returned path, since this path is internal storage.

Parameters
context Context
Returns
File The path of the directory holding application files that will not be automatically backed up to remote storage.

See also:

getObbDirs

File[] getObbDirs (Context context)

Returns absolute paths to application-specific directories on all external storage devices where the application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, these directories may not exist.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

  • External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
  • There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by StatFs.

Starting in KITKAT, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, WRITE_EXTERNAL_STORAGE is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using android:maxSdkVersion like this:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />

The first path returned is the same as getObbDir(). Returned paths may be null if a storage device is unavailable.

Parameters
context Context
Returns
File[]

See also:

isDeviceProtectedStorage

boolean isDeviceProtectedStorage (Context context)

Indicates if the storage APIs of this Context are backed by device-encrypted storage.

Parameters
context Context
Returns
boolean

See also:

startActivities

boolean startActivities (Context context, 
                Intent[] intents, 
                Bundle options)

Start a set of activities as a synthesized task stack, if able.

In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

startActivities provides a mechanism for constructing a synthetic task stack of multiple activities. If the underlying API is not available on the system this method will return false.

Parameters
context Context: Start activities using this activity as the starting context
intents Intent: Array of intents defining the activities that will be started. The element length-1 will correspond to the top activity on the resulting task stack.
options Bundle: Additional options for how the Activity should be started. See {@link android.content.Context#startActivity(Intent, android.os.Bundle)
Returns
boolean true if the underlying API was available and the call was successful, false otherwise

startActivities

boolean startActivities (Context context, 
                Intent[] intents)

Start a set of activities as a synthesized task stack, if able.

In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

startActivities provides a mechanism for constructing a synthetic task stack of multiple activities. If the underlying API is not available on the system this method will return false.

Parameters
context Context: Start activities using this activity as the starting context
intents Intent: Array of intents defining the activities that will be started. The element length-1 will correspond to the top activity on the resulting task stack.
Returns
boolean true if the underlying API was available and the call was successful, false otherwise
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.