Class AppState
- All Implemented Interfaces:
Externalizable
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAllows us to store an object state, this method must be implemented in order to save the state of an objectThe device this state was produced on.The object id must be unique, it is used to identify the object when loaded even when it is obfuscated.The application payload.The navigation stack as route paths, oldest first.longA counter that increases with every state this device publishes.longWhen this state was produced, as milliseconds since the epoch on the producing device.getTitle()A human readable label for what the user is doing, which a receiving device may show before they accept the continuation.intReturns 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 revisionsvoidinternalize(int version, DataInputStream in) Loads the object from the input stream and allows deserializationbooleanisEmpty()True when there is nothing here worth restoring or sending.setDeviceId(String id) Sets the originating device id.setPayload(Map<String, Object> p) Replaces the application payload.Replaces the route paths.setSequence(long s) Sets the sequence number.setTimestamp(long t) Sets the production timestamp.Sets the human readable label.toString()Returns a string representation of the object.
-
Constructor Details
-
AppState
public AppState()
-
-
Method Details
-
getRoutes
-
setRoutes
-
getPayload
-
setPayload
-
getDeviceId
The device this state was produced on. Used to drop a state's own echo when it comes back through a relay. Never null.
Returns
the originating device id
-
setDeviceId
-
getTitle
A human readable label for what the user is doing, which a receiving device may show before they accept the continuation. Null when the app did not set one.
Returns
the title, or null
-
setTitle
-
getSequence
public long getSequence()A counter that increases with every state this device publishes. Together with the device id it identifies a state exactly, which is how a receiver recognizes one it has already acted on -- two states can share a timestamp, because clocks are coarse.
Returns
the sequence number
-
setSequence
Sets the sequence number.
Parameters
s: the sequence number
Returns
this state, for chaining
-
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
the timestamp
-
setTimestamp
Sets the production timestamp.
Parameters
t: milliseconds since the epoch
Returns
this state, for chaining
-
isEmpty
public boolean isEmpty()True when there is nothing here worth restoring or sending.
Returns
true when both the routes and the payload are empty
-
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode()) -
getVersion
public int getVersion()Description copied from interface:ExternalizableReturns 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
Returns
version number for the persistant code
- Specified by:
getVersionin interfaceExternalizable
-
getObjectId
Description copied from interface:ExternalizableThe object id must be unique, it is used to identify the object when loaded even when it is obfuscated.
Returns
a unique id
- Specified by:
getObjectIdin interfaceExternalizable
-
externalize
Description copied from interface:ExternalizableAllows us to store an object state, this method must be implemented in order to save the state of an object
Parameters
out: the stream into which the object must be serialized
Throws
java.io.IOException: the method may throw an exception
- Specified by:
externalizein interfaceExternalizable- Throws:
IOException
-
internalize
Description copied from interface:ExternalizableLoads the object from the input stream and allows deserialization
Parameters
-
version: the version the class returned during the externalization processs -
in: the input stream used to load the class
Throws
java.io.IOException: the method may throw an exception
- Specified by:
internalizein interfaceExternalizable- Throws:
IOException
-
-