OLD | NEW |
(Empty) | |
| 1 <meta name="doc-family" content="apps"> |
| 2 <h1>Manage App Lifecycle</h1> |
| 3 |
| 4 |
| 5 <p> |
| 6 The app runtime and event page are responsible |
| 7 for managing the app lifecycle. |
| 8 The app runtime manages app installation, |
| 9 controls the event page, |
| 10 and can shutdown the app at anytime. |
| 11 The event page listens out for events from the app runtime |
| 12 and manages what gets launched and how. |
| 13 </p> |
| 14 |
| 15 <h2 id="lifecycle">How the lifecycle works</h2> |
| 16 |
| 17 <p> |
| 18 The app runtime loads the event page |
| 19 from a user's desktop and |
| 20 the <code>onLaunch()</code> event is fired. |
| 21 This event tells the event page what windows |
| 22 to launch and their dimensions. |
| 23 The lifecycle diagram here isn't the nicest to look at, |
| 24 but it's practical (and we will make it nicer soon). |
| 25 </p> |
| 26 |
| 27 <img src="{{static}}/images/applifecycle.png" |
| 28 width="444" |
| 29 height="329" |
| 30 alt="how app lifecycle works"> |
| 31 |
| 32 <p> |
| 33 When the event page has no executing JavaScript, |
| 34 no pending callbacks, and no open windows, |
| 35 the runtime unloads the event page and closes the app. |
| 36 Before unloading the event page, |
| 37 the <code>onSuspend()</code> event is fired. |
| 38 This gives the event page opportunity |
| 39 to do simple clean-up tasks |
| 40 before the app is closed. |
| 41 </p> |
| 42 |
| 43 <h2 id="eventpage">Create event page and windows</h2> |
| 44 |
| 45 <p> |
| 46 All apps must have an event page. |
| 47 This page contains the top-level logic of the application |
| 48 with none of its own UI and is responsible |
| 49 for creating the windows for all other app pages. |
| 50 </p> |
| 51 |
| 52 <h3>Create event page</h3> |
| 53 |
| 54 <p> |
| 55 To create the event page, |
| 56 include the "background" field in the app manifest |
| 57 and include the <code>background.js</code> in the scripts array. |
| 58 Any library scripts used by the event page need to be added |
| 59 to the "background" field first: |
| 60 </p> |
| 61 |
| 62 <pre> |
| 63 "background": { |
| 64 "scripts": [ |
| 65 "foo.js", |
| 66 "background.js" |
| 67 ] |
| 68 } |
| 69 </pre> |
| 70 |
| 71 <p> |
| 72 Your event page must include the <code>onLaunched()</code> function. |
| 73 This function is called |
| 74 when your application is launched in any way: |
| 75 </p> |
| 76 |
| 77 <pre> |
| 78 chrome.experimental.app.onLaunched.addListener(function() { |
| 79 // Tell your app what to launch and how. |
| 80 }); |
| 81 </pre> |
| 82 |
| 83 <h3>Create windows</h3> |
| 84 |
| 85 <p> |
| 86 An event page may create one or more windows at its discretion. |
| 87 By default, these windows are created with a script connection |
| 88 to the event page and are directly scriptable by the event page. |
| 89 </p> |
| 90 |
| 91 <p> |
| 92 Windows can either be shells or panels. |
| 93 Shell windows have no browser chrome. |
| 94 Panel windows are the same as shell windows, |
| 95 except they have different size and position restrictions, |
| 96 for example, a chat panel. |
| 97 </p> |
| 98 |
| 99 <p>Here's a sample <code>background.js</code> |
| 100 with a 'shell' window:</p> |
| 101 |
| 102 <pre> |
| 103 chrome.experimental.app.onLaunched.addListener(function() { |
| 104 chrome.app.window.create('main.html', { |
| 105 width: 800, |
| 106 height: 600, |
| 107 minWidth: 800, |
| 108 minHeight: 600, |
| 109 left: 100, |
| 110 top: 100, |
| 111 type: 'shell' |
| 112 }); |
| 113 }); |
| 114 </pre> |
| 115 |
| 116 <p>Here's a sample <code>background.js</code> |
| 117 with a 'panel' window: |
| 118 </p> |
| 119 |
| 120 <pre> |
| 121 chrome.experimental.app.onLaunched.addListener(function() { |
| 122 chrome.app.window.create('index.html', { |
| 123 width: 400, |
| 124 height: 200, |
| 125 type: 'panel' |
| 126 }); |
| 127 }); |
| 128 </pre> |
| 129 |
| 130 <h3>Including Launch Data</h3> |
| 131 |
| 132 <p> |
| 133 Depending on how your app is launched, |
| 134 you may need to include launch data |
| 135 in your event page. |
| 136 By default, there is no launch data |
| 137 when the app is started by the app launcher. |
| 138 For apps that provide intents, |
| 139 you need to include the |
| 140 <code>launchData.intent</code> parameter. |
| 141 </p> |
| 142 |
| 143 <p> |
| 144 Web intents can be launched by other apps invoking their intent, |
| 145 or by the runtime when apps are launched to view or edit files, |
| 146 for example from the operating system file explorer. |
| 147 To find out how to launch an app with web intents, |
| 148 see <a href="app_intents.html#launching">Launching an App with a File</a>. |
| 149 </p> |
| 150 |
| 151 <h2 id="runtime">Listening for app runtime events</h2> |
| 152 |
| 153 <p> |
| 154 The app runtime controls the app installs, updates, and uninstalls. |
| 155 You don't need to do anything to set up the app runtime, |
| 156 but your event page can listen out for the <code>onInstalled()</code> event |
| 157 to store local settings and the |
| 158 <code>onSuspend()</code> event to do simple clean-up tasks |
| 159 before the event page is unloaded. |
| 160 </p> |
| 161 |
| 162 <h3>Storing local settings</h3> |
| 163 |
| 164 <p> |
| 165 <code>chrome.runtime.onInstalled()</code> |
| 166 is called when your app has first been installed, |
| 167 or when it has been updated. |
| 168 Any time this function is called, |
| 169 the <code>onInstalled</code> event is fired. |
| 170 The event page can listen for this event and use the |
| 171 <a href="storage.html">Storage API</a> |
| 172 to store and update local settings |
| 173 (see also <a href="app_storage.html#options">Storage options</a>). |
| 174 </p> |
| 175 |
| 176 <pre> |
| 177 chrome.runtime.onInstalled.addListener(function() { |
| 178 chrome.storage.local.set(object items, function callback); |
| 179 }); |
| 180 </pre> |
| 181 |
| 182 <h3>Preventing data loss</h3> |
| 183 |
| 184 <p> |
| 185 Users can uninstall your app at any time. |
| 186 When uninstalled, |
| 187 no executing code or private data is left behind. |
| 188 This can lead to data loss |
| 189 since the users may be uninstalling an app |
| 190 that has locally edited, unsynchronized data. |
| 191 You should stash data to prevent data loss. |
| 192 </p> |
| 193 |
| 194 <p> |
| 195 At a minimum, |
| 196 you should store user settings |
| 197 so that if users reinstall your app, |
| 198 their information is still available for reuse. |
| 199 Using the Storage API |
| 200 (<a href="storage.html#property-sync">chrome.storage.sync</a>), |
| 201 user data can be automatically synced |
| 202 with Chrome sync. |
| 203 </p> |
| 204 |
| 205 <h3>Clean-up before app closes</h3> |
| 206 |
| 207 <p> |
| 208 The app runtime sends the <code>onSuspend()</code> |
| 209 event to the event page before unloading it. |
| 210 Your event page can listen out for this event and |
| 211 do clean-up tasks before the app closes. |
| 212 </p> |
| 213 |
| 214 <p> |
| 215 Once this event is fired, |
| 216 the app runtime starts the process of closing the app: |
| 217 all events stop firing and |
| 218 JavaScript execution is halted. |
| 219 Any asynchronous operations started |
| 220 while handling this event are not guaranteed to complete. |
| 221 Keep the clean-up tasks synchronous and simple. |
| 222 </p> |
| 223 |
| 224 <pre> |
| 225 chrome.runtime.onSuspend.addListener(function() { |
| 226 // Do some simple clean-up tasks. |
| 227 }); |
| 228 </pre> |
| 229 |
| 230 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |