public
class
FragmentCompat
extends Object
java.lang.Object | |
↳ | android.support.v13.app.FragmentCompat |
Helper for accessing features in Fragment
introduced after
API level 13 in a backwards compatible fashion.
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)
Call |
static
void
|
setUserVisibleHint(Fragment f, boolean deferStart)
Call |
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
|
FragmentCompat ()
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[]) . |
void setMenuVisibility (Fragment f, boolean visible)
Call Fragment.setMenuVisibility(boolean)
if running on an appropriate version of the platform.
Parameters | |
---|---|
f |
Fragment
|
visible |
boolean
|
void setUserVisibleHint (Fragment f, boolean deferStart)
Call setUserVisibleHint(boolean)
if running on an appropriate version of the platform.
Parameters | |
---|---|
f |
Fragment
|
deferStart |
boolean
|
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. |