FragmentCompat

open class FragmentCompat
kotlin.Any
   ↳ androidx.legacy.app.FragmentCompat

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

Summary

Nested classes
abstract

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

abstract

Customizable delegate that allows delegating permission related compatibility methods to a custom implementation.

Public constructors

Public methods
open static Unit
requestPermissions(@NonNull fragment: Fragment, @NonNull permissions: Array<String!>, requestCode: Int)

Requests permissions to be granted to this application.

open static Unit

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

open static Unit

Sets the permission delegate for FragmentCompat.

open static Unit
setUserVisibleHint(f: Fragment!, deferStart: Boolean)

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

open static Boolean
shouldShowRequestPermissionRationale(@NonNull fragment: Fragment, @NonNull permission: String)

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

Public constructors

<init>

FragmentCompat()

Deprecated: Use androidx.fragment.app.Fragment instead of the framework fragment.

Public methods

requestPermissions

open static fun requestPermissions(@NonNull fragment: Fragment, @NonNull permissions: Array<String!>, requestCode: Int): Unit

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 android.content.pm.PermissionInfo#PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions android.content.pm.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 and the results of permission requests will be delivered to its 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 .

Parameters
fragment Fragment: The target fragment.
permissions Fragment: The requested permissions.
requestCode Fragment: Application specific request code to match with a result reported to OnRequestPermissionsResultCallback#onRequestPermissionsResult( * int, String[], int[]).

setMenuVisibility

open static fun setMenuVisibility(f: Fragment!, visible: Boolean): Unit

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

setPermissionCompatDelegate

open static fun setPermissionCompatDelegate(delegate: FragmentCompat.PermissionCompatDelegate!): Unit

Sets the permission delegate for FragmentCompat. Replaces the previously set delegate.

Parameters
delegate FragmentCompat.PermissionCompatDelegate!: The delegate to be set. null to clear the set delegate.

setUserVisibleHint

open static fun setUserVisibleHint(f: Fragment!, deferStart: Boolean): Unit

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

shouldShowRequestPermissionRationale

open static fun shouldShowRequestPermissionRationale(@NonNull fragment: Fragment, @NonNull permission: String): Boolean

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 Fragment: A permission your app wants to request.
Return
Boolean: Whether you can show permission rationale UI.