Modifier and Type | Class and Description |
---|---|
static interface |
FragmentCompat.OnRequestPermissionsResultCallback
This interface is the contract for receiving the results for permission requests.
|
Constructor and Description |
---|
FragmentCompat() |
Modifier and Type | Method and Description |
---|---|
static void |
requestPermissions(Fragment fragment,
String[] permissions,
int requestCode)
Requests permissions to be granted to this application.
|
static void |
setMenuVisibility(Fragment f,
boolean visible)
Call
Fragment.setMenuVisibility(boolean)
if running on an appropriate version of the platform. |
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.
|
public static void setMenuVisibility(Fragment f, boolean visible)
Fragment.setMenuVisibility(boolean)
if running on an appropriate version of the platform.public static void setUserVisibleHint(Fragment f, boolean deferStart)
setUserVisibleHint(boolean)
if running on an appropriate version of the platform.public static void requestPermissions(@NonNull Fragment fragment, @NonNull String[] permissions, int requestCode)
#PROTECTION_DANGEROUS dangerous
, regardless whether they are declared by
the platform or a third-party app.
Normal permissions PermissionInfo.PROTECTION_NORMAL
are granted at install time if requested in the manifest. Signature permissions
PermissionInfo.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
FragmentCompat.OnRequestPermissionsResultCallback.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 ContextCompat.checkSelfPermission(
android.content.Context, String)
.
fragment
- The target fragment.permissions
- The requested permissions.requestCode
- Application specific request code to match with a result
reported to FragmentCompat.OnRequestPermissionsResultCallback.onRequestPermissionsResult(
int, String[], int[])
.ContextCompat.checkSelfPermission(
android.content.Context, String)
,
shouldShowRequestPermissionRationale(android.app.Fragment, String)
public static boolean shouldShowRequestPermissionRationale(@NonNull Fragment fragment, @NonNull String 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.
fragment
- The target fragment.permission
- A permission your app wants to request.ContextCompat.checkSelfPermission(
android.content.Context, String)
,
requestPermissions(android.app.Fragment, String[], int)