# FragmentFactory

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/fragment/app/FragmentFactory "View this page in Java")

<br />

```
class FragmentFactory
```

<br />

*** ** * ** ***

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

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

## Summary

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

|                                                                                ### Public functions                                                                                 |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Fragment](https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment)                                                                                           | [instantiate](https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory#instantiate(java.lang.ClassLoader,java.lang.String))`(classLoader: `[ClassLoader](https://developer.android.com/reference/java/lang/ClassLoader.html)`, className: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` Create a new instance of a Fragment with the given class name. |
| `java-static `[Class](https://developer.android.com/reference/java/lang/Class.html)`<`[Fragment](https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment)`!>` | [loadFragmentClass](https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory#loadFragmentClass(java.lang.ClassLoader,java.lang.String))`(classLoader: `[ClassLoader](https://developer.android.com/reference/java/lang/ClassLoader.html)`, className: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` Parse a Fragment Class from the given class name.  |

## Public constructors

### FragmentFactory

Added in 1.1.0  

```
FragmentFactory()
```  

## Public functions

### instantiate

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

```
fun instantiate(classLoader: ClassLoader, className: String): Fragment
```

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

|                                            Parameters                                            |
|--------------------------------------------------------------------------------------------------|--------------------------------------------------|
| `classLoader: `[ClassLoader](https://developer.android.com/reference/java/lang/ClassLoader.html) | The default classloader to use for instantiation |
| `className: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)   | The class name of the fragment to instantiate.   |

|                                          Returns                                          |
|-------------------------------------------------------------------------------------------|----------------------------------|
| [Fragment](https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment) | Returns a new fragment instance. |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| [androidx.fragment.app.Fragment.InstantiationException](https://developer.android.com/reference/kotlin/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)  

```
java-static fun loadFragmentClass(classLoader: ClassLoader, className: String): Class<Fragment!>
```

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

|                                            Parameters                                            |
|--------------------------------------------------------------------------------------------------|------------------------------------------------------|
| `classLoader: `[ClassLoader](https://developer.android.com/reference/java/lang/ClassLoader.html) | The default classloader to use for loading the Class |
| `className: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)   | The class name of the fragment to parse.             |

|                                                                                Returns                                                                                |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|
| [Class](https://developer.android.com/reference/java/lang/Class.html)`<`[Fragment](https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment)`!>` | Returns the parsed Fragment Class |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| [androidx.fragment.app.Fragment.InstantiationException](https://developer.android.com/reference/kotlin/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. |