public abstract class NavType<T extends Object>

Known direct subclasses
CollectionNavType

A NavType for Collection such as arrays, lists, maps.


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.

Parameters
<T extends Object>

the type of the data that is supported by this NavType

Summary

Nested types

public static class NavType.Companion

Public constructors

<T extends Object> NavType(boolean isNullableAllowed)

Public methods

static @NonNull NavType<@NonNull ?>
fromArgType(String type, String packageName)

Parse an argType string into a NavType.

abstract T
get(@NonNull SavedState bundle, @NonNull String key)

Get a value of this type from the savedState

@NonNull String

The name of this type.

boolean

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

abstract @NonNull T

Parse a value of this type from a String.

@NonNull T
parseValue(@NonNull String value, @NonNull T previousValue)

Parse a value of this type from a String and then combine that parsed value with the given previousValue of the same type to provide a new value that contains both the new and previous value.

abstract void
put(@NonNull SavedState bundle, @NonNull String key, @NonNull T value)

Put a value of this type in the savedState

@NonNull String

Serialize a value of this NavType into a String.

boolean
valueEquals(@NonNull T value, @NonNull T other)

Compares two values of type T and returns true if values are equal.

Public constructors

public <T extends Object> NavType(boolean isNullableAllowed)
Parameters
<T extends Object>

the type of the data that is supported by this NavType

boolean isNullableAllowed

whether an argument with this type can hold a null value.

Public methods

fromArgType

Added in 2.4.0
public static @NonNull NavType<@NonNull ?> fromArgType(String type, String packageName)

Parse an argType string into a NavType.

Parameters
String type

argType string, usually parsed from the Navigation XML file

String packageName

package name of the R file, used for parsing relative class names starting with a dot.

Returns
@NonNull NavType<@NonNull ?>

a NavType representing the type indicated by the argType string. Defaults to StringType for null.

Throws
kotlin.IllegalArgumentException

if there is no valid argType

kotlin.RuntimeException

if the type class name cannot be found

get

public abstract T get(@NonNull SavedState bundle, @NonNull String key)

Get a value of this type from the savedState

Parameters
@NonNull SavedState bundle

savedState to get value from

@NonNull String key

savedState key

Returns
T

value of this type

getName

Added in 1.0.0
public @NonNull String getName()

The name of this type.

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

Returns
@NonNull String

name of this type

isNullableAllowed

Added in 1.0.0
public boolean isNullableAllowed()

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

Returns
boolean

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

parseValue

Added in 1.0.0
public abstract @NonNullparseValue(@NonNull String value)

Parse a value of this type from a String.

Parameters
@NonNull String value

string representation of a value of this type

Returns
@NonNull T

parsed value of the type represented by this NavType

Throws
kotlin.IllegalArgumentException

if value cannot be parsed into this type

parseValue

Added in 2.6.0
public @NonNullparseValue(@NonNull String value, @NonNull T previousValue)

Parse a value of this type from a String and then combine that parsed value with the given previousValue of the same type to provide a new value that contains both the new and previous value.

By default, the given value will replace the previousValue.

Parameters
@NonNull String value

string representation of a value of this type

@NonNull T previousValue

previously parsed value of this type

Returns
@NonNull T

combined parsed value of the type represented by this NavType

Throws
kotlin.IllegalArgumentException

if value cannot be parsed into this type

put

public abstract void put(@NonNull SavedState bundle, @NonNull String key, @NonNull T value)

Put a value of this type in the savedState

Parameters
@NonNull SavedState bundle

savedState to put value in

@NonNull String key

savedState key

@NonNull T value

value of this type

serializeAsValue

Added in 2.6.0
public @NonNull String serializeAsValue(@NonNull T value)

Serialize a value of this NavType into a String.

By default it returns value of kotlin.toString or null if value passed in is null.

This method can be override for custom serialization implementation on types such custom NavType classes.

Note: Final output should be encoded with NavUriUtils.encode

Parameters
@NonNull T value

a value representing this NavType to be serialized into a String

Returns
@NonNull String

encoded and serialized String value of value

valueEquals

Added in 2.8.0
public boolean valueEquals(@NonNull T value, @NonNull T other)

Compares two values of type T and returns true if values are equal.

Parameters
@NonNull T value

the first value for comparison

@NonNull T other

the second value for comparison