NavigationState


@AnyThread
public final class NavigationState


This class serves as an immutable snapshot of a Navigation instance at a certain point in time, captured every time a callback (e.g. onNavigationStarted) is called.

Note: Do not use NavigationState as a key to connect callbacks, use Navigation instead from getNavigation.

Summary

Public methods

boolean

Indicates whether the navigation committed (i.e., did not get aborted/return 204/etc).

boolean

Indicates whether the navigation committed an error page.

@NonNull Navigation

Returns the navigation linked to this NavigationState snapshot

long
@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_NAVIGATION_START_UPTIME_MILLIS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getNavigationStartUptimeMillis()

Returns the timestamp when this navigation was initiated.

@Nullable PageState

Returns the PageState that the navigation commits into.

@Nullable Map<String, String>
@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_RESPONSE_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getResponseHeaders()

Returns the HTTP response headers received for this navigation.

int

Returns the status code received by the navigation.

@NonNull String

Returns the URL of the Navigation's state at a point in time

@Nullable WebResourceErrorCompat
@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_WEB_RESOURCE_ERROR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebResourceError()

Navigation error information for the navigation load.

boolean

Indicates whether the navigation is a history back navigation.

boolean

Indicates whether the navigation is a history forward navigation.

boolean

Indicates whether the navigation is a history navigation.

boolean

Indicates whether the navigation is a reload navigation.

boolean

Indicates whether the navigation is a restore navigation after calling restoreState.

boolean

Indicates whether the navigation is a same-document navigation.

boolean

Indicates whether the navigation is initiated by the page/renderer (e.g., link clicks, JS script) instead of the browser/app (e.g., loadUrl calls).

Public methods

didCommit

public boolean didCommit()

Indicates whether the navigation committed (i.e., did not get aborted/return 204/etc).

Note: This will return true if didCommitErrorPage returns true.

Returns
boolean

True if committed, false otherwise.

didCommitErrorPage

public boolean didCommitErrorPage()

Indicates whether the navigation committed an error page.

Returns
boolean

True if an error page was committed, false otherwise.

getNavigation

public @NonNull Navigation getNavigation()

Returns the navigation linked to this NavigationState snapshot

getNavigationStartUptimeMillis

@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_NAVIGATION_START_UPTIME_MILLIS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public long getNavigationStartUptimeMillis()

Returns the timestamp when this navigation was initiated.

The returned timestamp represents the time elapsed since device boot in milliseconds, excluding time spent in deep sleep. This timebase is equivalent to uptimeMillis.

Returns
long

The start time of the navigation in uptime milliseconds.

getPageState

public @Nullable PageState getPageState()

Returns the PageState that the navigation commits into.

Note: This method will return null if the navigation had not committed a page when this snapshot was taken, or never commits one (204/download). If the navigation successfully commits a page, this method will return the corresponding PageState object for the page. This could wrap a newly created Page or a previously seen Page in the case of BFCache (Back/Forward Cache).

Note: Once this method returns a non-null PageState object for a specific navigation's Page, getPage will always return the same Page object for that navigation.

Returns
@Nullable PageState

The PageState object, or null if the navigation had not committed a Page when the snapshot was taken. (e.g., 204/download).

getResponseHeaders

@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_RESPONSE_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public @Nullable Map<String, String> getResponseHeaders()

Returns the HTTP response headers received for this navigation.

The response headers are not available until the navigation has completed, so this method will return null until onNavigationCompleted has been called for this navigation.

The returned map will be empty if the navigation did not receive an HTTP response, for example for same-document navigations, navigations to non-HTTP URLs (such as file:// or about:blank) or navigations that failed before a response was received.

The Set-Cookie header is never included. Only the headers of the final response are returned, the headers of any redirects are exposed via getResponseHeaders in onNavigationRedirected

Returns
@Nullable Map<String, String>

An unmodifiable Map from header name to header value, or null if the navigation has not completed yet.

Throws
java.lang.UnsupportedOperationException

if the NAVIGATION_GET_RESPONSE_HEADERS feature is not supported. This should be checked before use with isFeatureSupported.

getStatusCode

public int getStatusCode()

Returns the status code received by the navigation.

Returns
int

The HTTP status code.

getUrl

public @NonNull String getUrl()

Returns the URL of the Navigation's state at a point in time

Returns
@NonNull String

The URL of this NavigationState as a String.

getWebResourceError

@RequiresFeature(name = WebViewFeature.NAVIGATION_GET_WEB_RESOURCE_ERROR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public @Nullable WebResourceErrorCompat getWebResourceError()

Navigation error information for the navigation load.

Returns
@Nullable WebResourceErrorCompat

The WebResourceErrorCompat object, or null if there is no error for this navigation.

Throws
java.lang.UnsupportedOperationException

if the NAVIGATION_GET_WEB_RESOURCE_ERROR feature is not supported. This should be checked before use with isFeatureSupported.

isBack

public boolean isBack()

Indicates whether the navigation is a history back navigation.

Returns
boolean

True if back navigation, false otherwise.

isForward

public boolean isForward()

Indicates whether the navigation is a history forward navigation.

Returns
boolean

True if forward navigation, false otherwise.

isHistory

public boolean isHistory()

Indicates whether the navigation is a history navigation.

Returns
boolean

True if history, false otherwise.

isReload

public boolean isReload()

Indicates whether the navigation is a reload navigation.

Returns
boolean

True if reload, false otherwise.

isRestore

public boolean isRestore()

Indicates whether the navigation is a restore navigation after calling restoreState.

Returns
boolean

True if session restore, false otherwise.

isSameDocument

public boolean isSameDocument()

Indicates whether the navigation is a same-document navigation.

Returns
boolean

True if same-document, false otherwise.

wasInitiatedByPage

public boolean wasInitiatedByPage()

Indicates whether the navigation is initiated by the page/renderer (e.g., link clicks, JS script) instead of the browser/app (e.g., loadUrl calls).

Returns
boolean

True if page-initiated, false otherwise.