# FragmentFactory

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

*** ** * ** ***


```
public class FragmentFactory
```

<br />

*** ** * ** ***

Interface used to control the instantiation of `https://developer.android.com/reference/androidx/fragment/app/Fragment` instances. Implementations can be registered with a `https://developer.android.com/reference/androidx/fragment/app/FragmentManager` via `https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setFragmentFactory(androidx.fragment.app.FragmentFactory)`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setFragmentFactory(androidx.fragment.app.FragmentFactory)` |   |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory#FragmentFactory()()` |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/fragment/app/Fragment` | `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory#instantiate(java.lang.ClassLoader,java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/ClassLoader.html classLoader, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html className)` Create a new instance of a Fragment with the given class name. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/fragment/app/Fragment>` | `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory#loadFragmentClass(java.lang.ClassLoader,java.lang.String)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/ClassLoader.html classLoader, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html className )` Parse a Fragment Class from the given class name. |

## Public constructors

### FragmentFactory

Added in 1.1.0

```
public FragmentFactory()
```

## Public methods

### instantiate

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

```
public @NonNull Fragment instantiate(@NonNull ClassLoader classLoader, @NonNull String className)
```

Create a new instance of a Fragment with the given class name. This uses `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory#loadFragmentClass(java.lang.ClassLoader,java.lang.String)` and the empty constructor of the resulting Class by default.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/ClassLoader.html classLoader` | The default classloader to use for instantiation |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html className` | The class name of the fragment to instantiate. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/fragment/app/Fragment` | Returns a new fragment instance. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/fragment/app/Fragment.InstantiationException` | If there is a failure in instantiating the given fragment class. This is a runtime exception; it is not normally expected to happen. |

### loadFragmentClass

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

```
public static @NonNull Class<Fragment> loadFragmentClass(
    @NonNull ClassLoader classLoader,
    @NonNull String className
)
```

Parse a Fragment Class from the given class name. The resulting Class is kept in a global cache, bypassing the `https://developer.android.com/reference/java/lang/Class.html#forName(java.lang.String)` calls when passed the same class name again.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/ClassLoader.html classLoader` | The default classloader to use for loading the Class |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html className` | The class name of the fragment to parse. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/fragment/app/Fragment>` | Returns the parsed Fragment Class |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/fragment/app/Fragment.InstantiationException` | If there is a failure in parsing the given fragment class. This is a runtime exception; it is not normally expected to happen. |