# FragmentHostCallback

Artifact: [androidx.fragment:fragment](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/FragmentHostCallback.kt+class:androidx.fragment.app.FragmentHostCallback) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

*** ** * ** ***

<br />

```
public abstract class FragmentHostCallback<H extends Object> extends FragmentContainer
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.fragment.app.FragmentContainer](https://developer.android.com/reference/androidx/fragment/app/FragmentContainer) ||
|   | ↳ | [androidx.fragment.app.FragmentHostCallback](https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback) |

*** ** * ** ***

Integration points with the Fragment host.

Fragments may be hosted by any object; such as an `https://developer.android.com/reference/android/app/Activity.html`. In order to host fragments, implement `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback`, overriding the methods applicable to the host.

FragmentManager changes its behavior based on what optional interfaces your FragmentHostCallback implements. This includes the following:

- **`https://developer.android.com/reference/androidx/activity/result/ActivityResultRegistryOwner`**: Removes the need to override .onStartIntentSenderFromFragment or .onRequestPermissionsFromFragment.

- **`https://developer.android.com/reference/androidx/fragment/app/FragmentOnAttachListener`** : Removes the need to manually call `https://developer.android.com/reference/androidx/fragment/app/FragmentManager#addFragmentOnAttachListener(androidx.fragment.app.FragmentOnAttachListener)` from your host in order to receive `https://developer.android.com/reference/androidx/fragment/app/FragmentOnAttachListener#onAttachFragment(androidx.fragment.app.FragmentManager,androidx.fragment.app.Fragment)` callbacks for the `https://developer.android.com/reference/androidx/fragment/app/FragmentController#getSupportFragmentManager()`.

- **`https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcherOwner`** : Removes the need to manually call `https://developer.android.com/reference/androidx/fragment/app/FragmentManager#popBackStackImmediate()` when handling the system back button.

- **`https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`** : Removes the need for your `https://developer.android.com/reference/androidx/fragment/app/FragmentController` to call `https://developer.android.com/reference/androidx/fragment/app/FragmentController#retainNestedNonConfig()` or `https://developer.android.com/reference/androidx/fragment/app/FragmentController#restoreAllState(android.os.Parcelable,java.util.List<androidx.fragment.app.Fragment>)`.

| Parameters |
|---|---|
| `<H extends https://developer.android.com/reference/java/lang/Object.html>` | the type of object that's currently hosting the fragments. An instance of this class must be returned by `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onGetHost()`. |

## Summary

| ### Public constructors |
|---|
| `<H extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#FragmentHostCallback(android.content.Context,android.os.Handler,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/os/Handler.html handler, int windowAnimations )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onDump(kotlin.String,java.io.FileDescriptor,java.io.PrintWriter,kotlin.Array)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html prefix, https://developer.android.com/reference/java/io/FileDescriptor.html fd, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/PrintWriter.html writer, String[] args )` Print internal state into the given stream. |
| `https://developer.android.com/reference/android/view/View.html` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onFindViewById(kotlin.Int)(int id)` Return the view with the given resource ID. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull H` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onGetHost()()` Return the object that's currently hosting the fragment. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/LayoutInflater.html` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onGetLayoutInflater()()` Return a `https://developer.android.com/reference/android/view/LayoutInflater.html`. |
| `int` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onGetWindowAnimations()()` Return the window animations. |
| `boolean` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onHasView()()` Return `true` if the container holds any view. |
| `boolean` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onHasWindowAnimations()()` Return `true` if there are window animations. |
| `void` | `[onRequestPermissionsFromFragment](https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onRequestPermissionsFromFragment(androidx.fragment.app.Fragment,kotlin.Array,kotlin.Int))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment, @https://developer.android.com/reference/androidx/annotation/NonNull String[] permissions, int requestCode )` **This method is deprecated.** Have your FragmentHostCallback implement {@link ActivityResultRegistryOwner} to allow Fragments to use {@link Fragment#registerForActivityResult(ActivityResultContract, ActivityResultCallback)} with {@link RequestMultiplePermissions}. |
| `boolean` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onShouldSaveFragmentState(androidx.fragment.app.Fragment)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment)` Return `true` if the fragment's state needs to be saved. |
| `boolean` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onShouldShowRequestPermissionRationale(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html permission)` Checks whether to show permission rationale UI from a fragment. |
| `void` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onStartActivityFromFragment(androidx.fragment.app.Fragment,android.content.Intent,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Intent.html intent, int requestCode )` Starts a new `https://developer.android.com/reference/android/app/Activity.html` from the given fragment. |
| `void` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onStartActivityFromFragment(androidx.fragment.app.Fragment,android.content.Intent,kotlin.Int,android.os.Bundle)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Intent.html intent, int requestCode, https://developer.android.com/reference/android/os/Bundle.html options )` Starts a new `https://developer.android.com/reference/android/app/Activity.html` from the given fragment. |
| `void` | `[onStartIntentSenderFromFragment](https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onStartIntentSenderFromFragment(androidx.fragment.app.Fragment,android.content.IntentSender,kotlin.Int,android.content.Intent,kotlin.Int,kotlin.Int,kotlin.Int,android.os.Bundle))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/IntentSender.html intent, int requestCode, https://developer.android.com/reference/android/content/Intent.html fillInIntent, int flagsMask, int flagsValues, int extraFlags, https://developer.android.com/reference/android/os/Bundle.html options )` **This method is deprecated.** Have your FragmentHostCallback implement {@link ActivityResultRegistryOwner} to allow Fragments to use {@link Fragment#registerForActivityResult(ActivityResultContract, ActivityResultCallback)} with {@link StartIntentSenderForResult}. |
| `void` | `https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onSupportInvalidateOptionsMenu()()` Invalidates the activity's options menu. |

### Inherited methods

From [androidx.fragment.app.FragmentContainer](https://developer.android.com/reference/androidx/fragment/app/FragmentContainer) `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/fragment/app/Fragment` `[instantiate](https://developer.android.com/reference/androidx/fragment/app/FragmentContainer#instantiate(android.content.Context,java.lang.String,android.os.Bundle))(

@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context,

@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html className,

@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html arguments

)`

**This method is deprecated.**

Use `https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setFragmentFactory(androidx.fragment.app.FragmentFactory)` to control how Fragments are instantiated.

<br />

## Public constructors

### FragmentHostCallback

```
public <H extends Object> FragmentHostCallback(
    @NonNull Context context,
    @NonNull Handler handler,
    int windowAnimations
)
```

## Public methods

### onDump

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public void onDump(
    @NonNull String prefix,
    FileDescriptor fd,
    @NonNull PrintWriter writer,
    String[] args
)
```

Print internal state into the given stream.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html prefix` | Desired prefix to prepend at each line of output. |
| `https://developer.android.com/reference/java/io/FileDescriptor.html fd` | The raw file descriptor that the dump is being sent to. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/PrintWriter.html writer` | The PrintWriter to which you should dump your state. This will be closed for you after you return. |
| `String[] args` | additional arguments to the dump request. |

### onFindViewById

```
public View onFindViewById(int id)
```

Return the view with the given resource ID. May return `null` if the view is not a child of this container.

### onGetHost

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public abstract @NonNull H onGetHost()
```

Return the object that's currently hosting the fragment. If a `https://developer.android.com/reference/androidx/fragment/app/Fragment` is hosted by a `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity`, the object returned here should be the same object returned from `https://developer.android.com/reference/androidx/fragment/app/Fragment#getActivity()`.

### onGetLayoutInflater

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public @NonNull LayoutInflater onGetLayoutInflater()
```

Return a `https://developer.android.com/reference/android/view/LayoutInflater.html`. See `https://developer.android.com/reference/android/app/Activity.html#getLayoutInflater()`.

### onGetWindowAnimations

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public int onGetWindowAnimations()
```

Return the window animations.

### onHasView

```
public boolean onHasView()
```

Return `true` if the container holds any view.

### onHasWindowAnimations

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public boolean onHasWindowAnimations()
```

Return `true` if there are window animations.

### onRequestPermissionsFromFragment

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0) Deprecated in [1.3.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0)

```
public void [onRequestPermissionsFromFragment](https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onRequestPermissionsFromFragment(androidx.fragment.app.Fragment,kotlin.Array,kotlin.Int))(
    @NonNull Fragment fragment,
    @NonNull String[] permissions,
    int requestCode
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Have your FragmentHostCallback implement {@link ActivityResultRegistryOwner} to allow Fragments to use {@link Fragment#registerForActivityResult(ActivityResultContract, ActivityResultCallback)} with {@link RequestMultiplePermissions}. This method will still be called when Fragments call the deprecated \<code\>requestPermissions()\</code\> method.

Requests permissions from the given fragment. See `https://developer.android.com/reference/android/app/Activity.html#requestPermissions(java.lang.String[], int)`

### onShouldSaveFragmentState

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public boolean onShouldSaveFragmentState(@NonNull Fragment fragment)
```

Return `true` if the fragment's state needs to be saved.

### onShouldShowRequestPermissionRationale

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public boolean onShouldShowRequestPermissionRationale(@NonNull String permission)
```

Checks whether to show permission rationale UI from a fragment. See `https://developer.android.com/reference/android/app/Activity.html#shouldShowRequestPermissionRationale(java.lang.String)`

### onStartActivityFromFragment

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public void onStartActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    int requestCode
)
```

Starts a new `https://developer.android.com/reference/android/app/Activity.html` from the given fragment. See `https://developer.android.com/reference/androidx/activity/ComponentActivity#startActivityForResult(android.content.Intent,kotlin.Int)`.

### onStartActivityFromFragment

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public void onStartActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    int requestCode,
    Bundle options
)
```

Starts a new `https://developer.android.com/reference/android/app/Activity.html` from the given fragment. See `https://developer.android.com/reference/androidx/activity/ComponentActivity#startActivityForResult(android.content.Intent,kotlin.Int)`.

### onStartIntentSenderFromFragment

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0) Deprecated in [1.3.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0)

```
public void [onStartIntentSenderFromFragment](https://developer.android.com/reference/androidx/fragment/app/FragmentHostCallback#onStartIntentSenderFromFragment(androidx.fragment.app.Fragment,android.content.IntentSender,kotlin.Int,android.content.Intent,kotlin.Int,kotlin.Int,kotlin.Int,android.os.Bundle))(
    @NonNull Fragment fragment,
    @NonNull IntentSender intent,
    int requestCode,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    Bundle options
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Have your FragmentHostCallback implement {@link ActivityResultRegistryOwner} to allow Fragments to use {@link Fragment#registerForActivityResult(ActivityResultContract, ActivityResultCallback)} with {@link StartIntentSenderForResult}. This method will still be called when Fragments call the deprecated \<code\>startIntentSenderForResult()\</code\> method.

Starts a new `https://developer.android.com/reference/android/content/IntentSender.html` from the given fragment. See `https://developer.android.com/reference/android/app/Activity.html#startIntentSender(android.content.IntentSender, android.content.Intent, int, int, int)`.

### onSupportInvalidateOptionsMenu

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0)

```
public void onSupportInvalidateOptionsMenu()
```

Invalidates the activity's options menu. See `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity#supportInvalidateOptionsMenu()`