Offline mode in Liferay Screens lets your apps function when connectivity is unavailable or intermittent. Even though the steady march of technology makes connections more stable and prevalent, there are still plenty of places the Internet has trouble reaching. Areas with complex terrain, including cities with large buildings, often lack stable connections. Remote areas typically don’t have connections at all. Using Screens’s offline mode in your apps gives your users flexibility in these situations.
This tutorial shows you how to use offline mode in Screenlets. For an explanation of how offline mode works, see the tutorial Architecture of Offline Mode in Liferay Screens. Offline mode’s architecture is the same on iOS and Android, although its use on these platforms differs.
Configuring Screenlets for Offline Mode
If you want to enable offline mode in any of your screenlets, you must configure the
offlinePolicy attribute when inserting the Screenlet’s XML in a layout. This attribute can take four possible values:
For a description of these values, see the section Using Policies with Offline Mode in the offline mode architecture tutorial. Note that each Screenlet behaves a bit differently with offline mode. For specific details, see the Screenlet reference documentation.
Under some scenarios, values stored in the local cache need to be synchronized with the portal. To do this, you need to use the
CacheSyncService class. This class sends information from the local cache to the portal. To register
CacheSyncService with your app, you must add the following code to your
<receiver android:name=".CacheReceiver"> <intent-filter> <action android:name="com.liferay.mobile.screens.auth.login.success"/> <action android:name="com.liferay.mobile.screens.cache.resync"/> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> <service android:name=".CacheSyncService" android:exported="false"/>
This code registers the
CacheSyncService components. The
CacheReceiver is invoked in the following scenarios:
- When a connectivity change occurs (for example, when the network connection is restored).
- When Login Screenlet successfully completes the login.
- When a specific
resyncintent is broadcasted. In this case, use
CacheSyncService performs the synchronization process when invoked from the above receiver. This is currently an unassisted process. Future versions will include some kind of control mechanism.