Skip to content

Most visited

Recently visited

navigation

FragmentCompat

public class FragmentCompat
extends Object

java.lang.Object
   ↳ android.support.v13.app.FragmentCompat


Helper for accessing features in Fragment in a backwards compatible fashion.

Summary

Nested classes

interface FragmentCompat.OnRequestPermissionsResultCallback

This interface is the contract for receiving the results for permission requests. 

Public constructors

FragmentCompat()

Public methods

static void requestPermissions(Fragment fragment, String[] permissions, int requestCode)

Requests permissions to be granted to this application.

static void setMenuVisibility(Fragment f, boolean visible)

This method was deprecated in API level 26.0.0. Use setMenuVisibility(boolean) directly.

static void setUserVisibleHint(Fragment f, boolean deferStart)

Call setUserVisibleHint(boolean) if running on an appropriate version of the platform.

static boolean shouldShowRequestPermissionRationale(Fragment fragment, String permission)

Gets whether you should show UI with rationale for requesting a permission.

Inherited methods

From class java.lang.Object

Public constructors

FragmentCompat

added in version 22.0.0
FragmentCompat ()

Public methods

requestPermissions

added in version 23.0.0
void requestPermissions (Fragment fragment, 
                String[] permissions, 
                int requestCode)

Requests permissions to be granted to this application. These permissions must be requested in your manifest, they should not be granted to your app, and they should have protection level #PROTECTION_DANGEROUS dangerous, regardless whether they are declared by the platform or a third-party app.

Normal permissions PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions PROTECTION_SIGNATURE are granted at install time if requested in the manifest and the signature of your app matches the signature of the app declaring the permissions.

If your app does not have the requested permissions the user will be presented with UI for accepting them. After the user has accepted or rejected the requested permissions you will receive a callback reporting whether the permissions were granted or not. Your fragment has to implement FragmentCompat.OnRequestPermissionsResultCallback and the results of permission requests will be delivered to its onRequestPermissionsResult(int, String[], int[]).

Note that requesting a permission does not guarantee it will be granted and your app should be able to run without having this permission.

This method may start an activity allowing the user to choose which permissions to grant and which to reject. Hence, you should be prepared that your activity may be paused and resumed. Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to your onRequestPermissionsResult( int, String[], int[]).

When checking whether you have a permission you should use checkSelfPermission(android.content.Context, String).

Parameters
fragment Fragment: The target fragment.

permissions String: The requested permissions.

requestCode int: Application specific request code to match with a result reported to onRequestPermissionsResult(int, String[], int[]).

See also:

setMenuVisibility

added in version 22.0.0
void setMenuVisibility (Fragment f, 
                boolean visible)

This method was deprecated in API level 26.0.0.
Use setMenuVisibility(boolean) directly.

Call Fragment.setMenuVisibility(boolean) if running on an appropriate version of the platform.

Parameters
f Fragment

visible boolean

setUserVisibleHint

added in version 22.0.0
void setUserVisibleHint (Fragment f, 
                boolean deferStart)

Call setUserVisibleHint(boolean) if running on an appropriate version of the platform.

Parameters
f Fragment

deferStart boolean

shouldShowRequestPermissionRationale

added in version 23.0.0
boolean shouldShowRequestPermissionRationale (Fragment fragment, 
                String permission)

Gets whether you should show UI with rationale for requesting a permission. You should do this only if you do not have the permission and the context in which the permission is requested does not clearly communicate to the user what would be the benefit from granting this permission.

For example, if you write a camera app, requesting the camera permission would be expected by the user and no rationale for why it is requested is needed. If however, the app needs location for tagging photos then a non-tech savvy user may wonder how location is related to taking photos. In this case you may choose to show UI with rationale of requesting this permission.

Parameters
fragment Fragment: The target fragment.

permission String: A permission your app wants to request.

Returns
boolean Whether you can show permission rationale UI.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

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.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)