public final class AppState
- Object
- AppState
ImplementsExternalizable
A snapshot of where the user was and what they were doing: the route stack, plus whatever your
StateProvider chose to add.
The same value serves three purposes, which is why it carries more than the two halves above.
It is written to storage so the app can come back after its process dies; it is advertised to
the user’s other devices so one of them can continue the work; and it travels through a
StateRelay to devices the platform cannot reach on its own. The deviceId, sequence and
timestamp are what let the receiving side tell a state it has already seen – or its own echo
– from one worth acting on.
The routes
getRoutes() is the com.codename1.router.Navigation stack as a list of paths, oldest first.
Restoring it re-runs each path through the route table, which is why an app that navigates with
@Route gets its screens back for free and one that calls new MyForm().show() does not: those
navigations are not URL-addressable, so there is nothing to write down. Such an app restores
from the payload instead.
The payload
getPayload() is yours. It has to survive being written to disk, handed to an operating system
and delivered to a different device running a possibly different build of your app, so it is
restricted to values that mean the same thing everywhere: String, Integer, Long, Double,
Boolean, and List and Map of those. Anything else is refused when the state is built,
with a message naming the offending key, rather than being dropped somewhere the failure cannot
be traced back here.
Constructors
public AppState() |
Methods
public List<String> getRoutes() | The navigation stack as route paths, oldest first. |
public AppState setRoutes(List<String> r) | Replaces the route paths. |
public Map<String, Object> getPayload() | The application payload. |
public AppState setPayload(Map<String, Object> p) | Replaces the application payload. |
public String getDeviceId() | The device this state was produced on. |
public AppState setDeviceId(String id) | Sets the originating device id. |
public String getTitle() | A human readable label for what the user is doing, which a receiving device may show before they accept the continuation. |
public AppState setTitle(String t) | Sets the human readable label. |
public long getSequence() | A counter that increases with every state this device publishes. |
public AppState setSequence(long s) | Sets the sequence number. |
public long getTimestamp() | When this state was produced, as milliseconds since the epoch on the producing device. |
public AppState setTimestamp(long t) | Sets the production timestamp. |
public boolean isEmpty() | True when there is nothing here worth restoring or sending. |
public String toString() | Returns a string representation of the object. |
public int getVersion() | Returns the version for the current persistance code, the version will be pased to internalized thus allowing the internalize method to recognize classes persisted in older revisions |
public String getObjectId() | The object id must be unique, it is used to identify the object when loaded even when it is obfuscated. |
public void externalize(DataOutputStream out)
throws IOException | Allows us to store an object state, this method must be implemented in order to save the state of an object |
public void internalize(int version, DataInputStream in)
throws IOException | Loads the object from the input stream and allows deserialization |
Inherited methods
Constructor details
AppState
public AppState()Method details
getRoutes
public List<String> getRoutes()Returns
setRoutes
public AppState setRoutes(List<String> r)Parameters
rList<String>- the paths, oldest first; null is treated as empty
Returns
getPayload
public Map<String, Object> getPayload()The application payload. Never null, possibly empty.
The view is unmodifiable ALL THE WAY DOWN. Wrapping only the outer map left every nested List and Map mutable, which matters most for an arrival: the same AppState handed to a listener or a provider is afterwards parked, persisted, acknowledged and published, so a caller that consumed a nested list – removing items as it applied them, which is an ordinary way to write that loop – changed the framework’s own snapshot of what arrived. setPayload() deep-copies on the way in for exactly this reason; the way out needed to match.
Returns
setPayload
public AppState setPayload(Map<String, Object> p)Parameters
pMap<String, Object>- the payload; null is treated as empty
Returns
Throws
IllegalArgumentException- when a value cannot cross to another device
getDeviceId
public String getDeviceId()Returns
setDeviceId
public AppState setDeviceId(String id)Parameters
idString- the id; null is treated as the empty string
Returns
getTitle
public String getTitle()Returns
setTitle
public AppState setTitle(String t)Parameters
tString- the title, or null for none
Returns
getSequence
public long getSequence()Returns
setSequence
public AppState setSequence(long s)Parameters
slong- the sequence number
Returns
getTimestamp
public long getTimestamp()When this state was produced, as milliseconds since the epoch on the producing device.
Treat it as advisory. It comes from another device’s clock, so it is only as trustworthy as that clock: it can be behind, ahead, or – across a daylight saving change or a manual correction – both within one session.
Returns
setTimestamp
public AppState setTimestamp(long t)Parameters
tlong- milliseconds since the epoch
Returns
isEmpty
public boolean isEmpty()Returns
toString
public String toString()getVersion
public int getVersion()Returns
getObjectId
public String getObjectId()Returns
externalize
public void externalize(DataOutputStream out)
throws IOExceptionParameters
outDataOutputStream- the stream into which the object must be serialized
Throws
java.io.IOException- the method may throw an exception
internalize
public void internalize(int version, DataInputStream in)
throws IOExceptionParameters
versionint- the version the class returned during the externalization processs
inDataInputStream- the input stream used to load the class
Throws
java.io.IOException- the method may throw an exception