NavType

abstract class NavType<T : Any!>
kotlin.Any
   ↳ androidx.navigation.NavType

NavType denotes the type that can be used in a NavArgument.

There are built-in NavTypes for primitive types, such as int, long, boolean, float, and strings, parcelable, and serializable classes (including Enums), as well as arrays of each supported type.

You should only use one of the static NavType instances and subclasses defined in this class.

Summary

Nested classes

EnumType is used for NavArguments holding enum values.

ParcelableArrayType is used for NavArguments which hold arrays of Parcelables.

ParcelableType is used for passing Parcelables in NavArguments.

SerializableArrayType is used for NavArguments that hold arrays of Serializables.

open

SerializableType is used for Serializable NavArguments.

Public methods

open static NavType<*>
fromArgType(@Nullable type: String?, @Nullable packageName: String?)

Parse an argType string into a NavType.

abstract T?
get(@NonNull bundle: Bundle, @NonNull key: String)

Get a value of this type from the bundle

abstract String

Returns the name of this type.

open Boolean

Check if an argument with this type can hold a null value.

abstract T
parseValue(@NonNull value: String)

Parse a value of this type from a String.

abstract Unit
put(@NonNull bundle: Bundle, @NonNull key: String, @Nullable value: T?)

Put a value of this type in he bundle

open String

Properties

static NavType<BooleanArray!>

NavType for storing boolean arrays, corresponding with the "boolean[]" type in a Navigation XML file.

static NavType<Boolean!>

NavType for storing boolean values, corresponding with the "boolean" type in a Navigation XML file.

static NavType<FloatArray!>

NavType for storing float arrays, corresponding with the "float[]" type in a Navigation XML file.

static NavType<Float!>

NavType for storing float values, corresponding with the "float" type in a Navigation XML file.

static NavType<IntArray!>

NavType for storing integer arrays, corresponding with the "integer[]" type in a Navigation XML file.

static NavType<Int!>

NavType for storing integer values, corresponding with the "integer" type in a Navigation XML file.

static NavType<LongArray!>

NavType for storing long arrays, corresponding with the "long[]" type in a Navigation XML file.

static NavType<Long!>

NavType for storing long values, corresponding with the "long" type in a Navigation XML file.

static NavType<Int!>

NavType for storing integer values representing resource ids, corresponding with the "reference" type in a Navigation XML file.

static NavType<Array<String!>!>

NavType for storing String arrays, corresponding with the "string[]" type in a Navigation XML file.

static NavType<String!>

NavType for storing String values, corresponding with the "string" type in a Navigation XML file.

Public methods

fromArgType

@NonNull open static fun fromArgType(@Nullable type: String?, @Nullable packageName: String?): NavType<*>

Parse an argType string into a NavType.

Parameters
type String?: argType string, usually parsed from the Navigation XML file
packageName String?: package name of the R file, used for parsing relative class names starting with a dot.
Return
NavType<*>: a NavType representing the type indicated by the argType string. Defaults to StringType for null.

get

@Nullable abstract fun get(@NonNull bundle: Bundle, @NonNull key: String): T?

Get a value of this type from the bundle

Parameters
bundle Bundle: bundle to get value from
key Bundle: bundle key
Return
T?: value of this type

getName

@NonNull abstract fun getName(): String

Returns the name of this type.

This is the same value that is used in Navigation XML argType attribute.

Return
String: name of this type

isNullableAllowed

open fun isNullableAllowed(): Boolean

Check if an argument with this type can hold a null value.

Return
Boolean: Returns true if this type allows null values, false otherwise.

parseValue

@NonNull abstract fun parseValue(@NonNull value: String): T

Parse a value of this type from a String.

Parameters
value String: string representation of a value of this type
Return
T: parsed value of the type represented by this NavType
Exceptions
IllegalArgumentException if value cannot be parsed into this type

put

abstract fun put(@NonNull bundle: Bundle, @NonNull key: String, @Nullable value: T?): Unit

Put a value of this type in he bundle

Parameters
bundle Bundle: bundle to put value in
key Bundle: bundle key
value Bundle: value of this type

toString

@NonNull open fun toString(): String

Properties

BoolArrayType

@NonNull static val BoolArrayType: NavType<BooleanArray!>

NavType for storing boolean arrays, corresponding with the "boolean[]" type in a Navigation XML file.

Null values are supported. Default values in Navigation XML files are not supported.

BoolType

@NonNull static val BoolType: NavType<Boolean!>

NavType for storing boolean values, corresponding with the "boolean" type in a Navigation XML file.

Null values are not supported.

FloatArrayType

@NonNull static val FloatArrayType: NavType<FloatArray!>

NavType for storing float arrays, corresponding with the "float[]" type in a Navigation XML file.

Null values are supported. Default values in Navigation XML files are not supported.

FloatType

@NonNull static val FloatType: NavType<Float!>

NavType for storing float values, corresponding with the "float" type in a Navigation XML file.

Null values are not supported.

IntArrayType

@NonNull static val IntArrayType: NavType<IntArray!>

NavType for storing integer arrays, corresponding with the "integer[]" type in a Navigation XML file.

Null values are supported. Default values in Navigation XML files are not supported.

IntType

@NonNull static val IntType: NavType<Int!>

NavType for storing integer values, corresponding with the "integer" type in a Navigation XML file.

Null values are not supported.

LongArrayType

@NonNull static val LongArrayType: NavType<LongArray!>

NavType for storing long arrays, corresponding with the "long[]" type in a Navigation XML file.

Null values are supported. Default values in Navigation XML files are not supported.

LongType

@NonNull static val LongType: NavType<Long!>

NavType for storing long values, corresponding with the "long" type in a Navigation XML file.

Null values are not supported. Default values for this type in Navigation XML files must always end with an 'L' suffix, e.g. `app:defaultValue="123L"`.

ReferenceType

@NonNull static val ReferenceType: NavType<Int!>

NavType for storing integer values representing resource ids, corresponding with the "reference" type in a Navigation XML file.

Null values are not supported.

StringArrayType

@NonNull static val StringArrayType: NavType<Array<String!>!>

NavType for storing String arrays, corresponding with the "string[]" type in a Navigation XML file.

Null values are supported. Default values in Navigation XML files are not supported.

StringType

@NonNull static val StringType: NavType<String!>

NavType for storing String values, corresponding with the "string" type in a Navigation XML file.

Null values are supported.