public class Navigator extends Object
A navigator navigates by page key: the application calls a navigation method with the
int key of the page to show, and the navigator resolves the key through its
PageFactory. The public API accepts keys only; the application never constructs a
Page at a call site.
The navigator itself is not constructed by the application either: it is a singleton, configured
once with initialize(Desktop, PageFactory) and obtained with getInstance().
Every method invoked before initialization throws IllegalStateException.
The navigator owns a multi-level navigation history and drives the application-supplied MicroUI
Desktop it is initialized with: each page's content is mounted on it, and it is shown on
the first navigation. The application owns that desktop's stylesheet, render policy, and input
handling; the navigator exposes no styling method. Identity is per
entry — the same key may appear more than once in the history. Every navigation is
animated with a Transition (default Transition.IMMEDIATE).
navigateTo(int) pushes a new entry;navigateBack() pops the active entry;navigateBackTo(int) pops back to the nearest entry with a key;replaceWith(int) replaces the active page in place, discarding the replaced page.navigateTo(int) and replaceWith(int), also
come in an argument-carrying form: the argument is forwarded to
PageFactory.create(int, Object), which injects it into the page it builds, and the navigator
keeps no reference to it. The back navigations have no such form — they re-show the page the
history holds, without consulting the factory.
MicroUI must be started before the first navigation, and every method must be called from the
MicroUI thread. A navigation requested reentrantly — from a Page lifecycle hook or a
NavigationListener callback fired by an in-progress navigation — is not run inline
(which would corrupt the shared transition state); it is deferred with
MicroUI.callSerially(Runnable) and runs once the in-progress navigation has finished.
| Modifier and Type | Method and Description |
|---|---|
void |
addNavigationListener(NavigationListener listener)
Registers a navigation listener.
|
int |
getActiveKey()
Returns the key of the active page.
|
Page |
getActivePage()
Returns the active page, the page on top of the history.
|
List<Page> |
getHistory()
Returns a copy of the navigation history as pages, ordered from the oldest at index
0 to
the active page at the last index. |
int[] |
getHistoryKeys()
Returns a copy of the navigation history as keys, in the same order as
getHistory(). |
static Navigator |
getInstance()
Returns the navigator.
|
static void |
initialize(Desktop desktop,
PageFactory factory)
Initializes the navigator with the desktop it drives and the factory that builds its pages.
|
void |
navigateBack()
Navigates back, using the default transition.
|
void |
navigateBack(Transition transition)
Navigates back: the active entry is popped and the entry beneath it becomes the active page.
|
void |
navigateBackTo(int key)
Navigates back to the nearest page with the given key, using the default transition.
|
void |
navigateBackTo(int key,
Transition transition)
Navigates back to the nearest entry below the active one whose key matches; every entry above it
is removed from the history.
|
void |
navigateTo(int key)
Navigates to the page identified by the given key, using the default transition and carrying no
argument.
|
void |
navigateTo(int key,
Object argument)
Navigates to the page identified by the given key, carrying the given argument, using the default
transition.
|
void |
navigateTo(int key,
Object argument,
Transition transition)
Navigates to the page identified by the given key: it is built by the factory from the key and the
given argument, pushed on top of the history, and becomes the active page.
|
void |
navigateTo(int key,
Transition transition)
Navigates to the page identified by the given key, carrying no argument.
|
void |
removeNavigationListener(NavigationListener listener)
Unregisters a previously registered navigation listener.
|
void |
replaceWith(int key)
Replaces the active page with the page identified by the given key, using the default transition
and carrying no argument.
|
void |
replaceWith(int key,
Object argument)
Replaces the active page with the page identified by the given key, carrying the given argument,
using the default transition.
|
void |
replaceWith(int key,
Object argument,
Transition transition)
Replaces the active page with the page identified by the given key, built by the factory from the
key and the given argument, keeping the history depth unchanged.
|
void |
replaceWith(int key,
Transition transition)
Replaces the active page with the page identified by the given key, carrying no argument.
|
void |
setTransition(Transition transition)
Sets the default transition used by the no-transition navigation overloads.
|
public void addNavigationListener(NavigationListener listener)
A listener registered while a navigation is being notified is not notified for that navigation: the listeners notified for a navigation are those registered when its notification pass starts.
listener - the listener to register.NullPointerException - if the given listener is null.IllegalStateException - if the navigator has not been initialized.public int getActiveKey()
IllegalStateException - if the navigator has not been initialized, or if the history is empty (no navigation
has happened yet).public Page getActivePage()
IllegalStateException - if the navigator has not been initialized, or if the history is empty (no navigation
has happened yet).public List<Page> getHistory()
0 to
the active page at the last index.IllegalStateException - if the navigator has not been initialized.public int[] getHistoryKeys()
getHistory(). A
key may appear more than once.IllegalStateException - if the navigator has not been initialized.public static Navigator getInstance()
The instance exists from class initialization on, but it is usable only once
initialize(Desktop, PageFactory) has been called: until then, every method it exposes
throws IllegalStateException.
public static void initialize(Desktop desktop, PageFactory factory)
Mounts the navigator's container on the given desktop and starts the navigator from a clean
state: an empty history, no registered NavigationListener, and Transition.IMMEDIATE
as the default transition. Calling this method again restarts the navigator from that same clean
state, on the newly given desktop and factory.
The application keeps ownership of the desktop: it applies the stylesheet, the render policy, and the input handling on it. The navigator shows it on the first navigation.
desktop - the desktop the navigator drives.factory - the factory building the pages the navigator navigates to.public void navigateBack()
IllegalStateException - if the navigator has not been initialized, or if there is nothing to go back to.public void navigateBack(Transition transition)
replaceWith(int) makes no difference — it discarded the page it replaced,
so back navigation reveals the entry beneath the replacing one.
The page returned to is the instance the history holds: the factory is not consulted, so a back navigation carries no argument.
transition - the transition to animate the change with.NullPointerException - if the given transition is null.IllegalStateException - if the navigator has not been initialized, or if there is no entry beneath the active
one.public void navigateBackTo(int key)
key - the key of the page to navigate back to.IllegalArgumentException - if no entry below the active one has the given key.IllegalStateException - if the navigator has not been initialized.public void navigateBackTo(int key,
Transition transition)
The page returned to is the instance the history holds: the factory is not consulted, so a back navigation carries no argument.
key - the key of the page to navigate back to.transition - the transition to animate the change with.NullPointerException - if the given transition is null.IllegalArgumentException - if no entry below the active one has the given key.IllegalStateException - if the navigator has not been initialized.public void navigateTo(int key)
key - the key of the page to navigate to.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void navigateTo(int key,
@Nullable
Object argument)
key - the key of the page to navigate to.argument - the argument to hand the factory, or null to carry none.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void navigateTo(int key,
@Nullable
Object argument,
Transition transition)
The argument is handed to PageFactory.create(int, Object) and nowhere else: the navigator
keeps no reference to it, so whatever the page needs from it, it keeps itself.
key - the key of the page to navigate to.argument - the argument to hand the factory, or null to carry none.transition - the transition to animate the change with.NullPointerException - if the given transition is null, or if the factory returns a null
page.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void navigateTo(int key,
Transition transition)
key - the key of the page to navigate to.transition - the transition to animate the change with.NullPointerException - if the given transition is null, or if the factory returns a null
page.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void removeNavigationListener(NavigationListener listener)
A listener unregistered while a navigation is being notified is still notified for that navigation, and no longer from the next one on.
listener - the listener to unregister.IllegalStateException - if the navigator has not been initialized.public void replaceWith(int key)
key - the key of the page to navigate to.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void replaceWith(int key,
@Nullable
Object argument)
key - the key of the page to navigate to.argument - the argument to hand the factory, or null to carry none.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void replaceWith(int key,
@Nullable
Object argument,
Transition transition)
The replaced page is discarded: the navigator keeps no reference to it, so the next
navigateBack() reveals the entry beneath the replacing one. The first navigation via this
method establishes its page as the root without an animation.
The argument is handed to PageFactory.create(int, Object) and nowhere else: the navigator
keeps no reference to it, so whatever the page needs from it, it keeps itself.
key - the key of the page to navigate to.argument - the argument to hand the factory, or null to carry none.transition - the transition to animate the change with.NullPointerException - if the given transition is null, or if the factory returns a null
page.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void replaceWith(int key,
Transition transition)
key - the key of the page to navigate to.transition - the transition to animate the change with.NullPointerException - if the given transition is null, or if the factory returns a null
page.IllegalArgumentException - if the factory does not recognize the given key.IllegalStateException - if the navigator has not been initialized.public void setTransition(Transition transition)
transition - the default transition to use.NullPointerException - if the given transition is null.IllegalStateException - if the navigator has not been initialized.