blob: b02c1d10f4d98cb8a65d7852e79f120453624849 [file] [log] [blame]
Scott Main57655252012-11-13 00:44:17 -08001page.title=Android 4.2 APIs
2sdk.platform.version=4.2
3sdk.platform.apiLevel=17
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8
9<h2>In this document</h2>
10<ol>
11 <li><a href="#Behaviors">Important Behavior Changes</a></li>
Scott Mainca211c72012-11-13 09:08:42 -080012 <li><a href="#Daydream">Daydream</a></li>
Scott Main57655252012-11-13 00:44:17 -080013 <li><a href="#SecondaryDisplays">Secondary Displays</a></li>
14 <li><a href="#Lockscreen">Lockscreen Widgets</a></li>
15 <li><a href="#MultipleUsers">Multiple Users</a></li>
16 <li><a href="#RTL">RTL Layout Support</a></li>
17 <li><a href="#NestedFragments">Nested Fragments</a></li>
18 <li><a href="#Renderscript">Renderscript</a></li>
19</ol>
20
21<h2>See also</h2>
22<ol>
23<li><a
24href="{@docRoot}sdk/api_diff/17/changes.html">API
25Differences Report &raquo;</a> </li>
26</ol>
27
28</div>
29</div>
30
31
32<p>API Level: 17</p>
33
34<p>Android 4.2 ({@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1})
Scott Mainca211c72012-11-13 09:08:42 -080035is an update to the Jelly Bean release that offers new features for users and app
Scott Main57655252012-11-13 00:44:17 -080036developers. This document provides an introduction to the most notable and
Scott Mainca211c72012-11-13 09:08:42 -080037useful new APIs for developers.</p>
38
39<p>As an app developer, you should download the Android 4.2 system image and SDK platform from
40the <a href="{@docRoot}tools/help/sdk-manager.html">SDK Manager</a> as soon as possible. If you
41don’t have a device running Android 4.2 on which to test your app, use the Android 4.2 system
42image to test your app on the <a href="{@docRoot}tools/devices/emulator.html">Android emulator</a>.
43Then build your apps against the Android 4.2 platform to begin using the latest APIs.</p>
44
Scott Main57655252012-11-13 00:44:17 -080045
46
47<div class="sidebox-wrapper">
48<div class="sidebox">
49
50<h3 id="ApiLevel">Declare your app API Level</h3>
51
52<p>To better optimize your app for devices running Android {@sdkPlatformVersion},
53 you should set your <a
54href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> to
55<code>"{@sdkPlatformApiLevel}"</code>, install it on an Android {@sdkPlatformVersion} system image,
56test it, then publish an update with this change.</p>
57
58<p>You
59can use APIs in Android {@sdkPlatformVersion} while also supporting older versions by adding
60conditions to your code that check for the system API level before executing
61APIs not supported by your <a
62href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a>.
63To learn more about
64maintaining backward-compatibility, read <a
65href="{@docRoot}training/backward-compatible-ui/index.html">Creating Backward-Compatible
66UIs</a>.</p>
67
68<p>More information about how API levels work is available in <a
69href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">What is API
70Level?</a></p>
71
72</div>
73</div>
74
75
Scott Main57655252012-11-13 00:44:17 -080076
77
78<h2 id="Behaviors">Important Behavior Changes</h2>
79
Scott Mainca211c72012-11-13 09:08:42 -080080<p>If you have previously published an app for Android, be aware of the following
81changes that might affect your app’s behavior:</p>
Scott Main57655252012-11-13 00:44:17 -080082
83<ul>
84 <li><b>Content providers</b> are no longer exported by default. That is, the default value
85for the <a href="{@docRoot}guide/topics/manifest/provider-element.html#exported">{@code
86android:exported}</a> attribute is now {@code “false"}. If it’s important that other apps be
87able to access your content provider, you must now explicitly set <a
88href="{@docRoot}guide/topics/manifest/provider-element.html#exported">{@code
89android:exported="true"}.</a>
90 <p>This change takes effect only if you set either <a
91 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code
92android:targetSdkVersion}</a> or <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
93android:minSdkVersion}</a> to 17 or higher. Otherwise, the default value is still {@code “true"}
94even when running on Android 4.2 and higher.</p>
95 </li>
96
97 <li>Compared to previous versions of Android, <b>user location</b> results may be less accurate
Scott Mainca211c72012-11-13 09:08:42 -080098if your app requests the {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} permission but
99does not request the {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission.
100 <p>To meet the privacy expectations of users when your app requests permission for
Scott Main57655252012-11-13 00:44:17 -0800101coarse location (and not fine location), the system will not provide a user location estimate
102that’s more accurate than a city block.</p>
103 </li>
104
105 <li>Some <b>device settings</b> defined by {@link android.provider.Settings.System} are now
106 read-only. If your app attempts to write changes to settings defined in {@link
107 android.provider.Settings.System} that have moved to {@link android.provider.Settings.Global},
108 the write operation will silently fail when running on Android 4.2 and higher.
109 <p>Even if your value for <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code
110android:targetSdkVersion}</a> and <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
111android:minSdkVersion}</a> is lower than 17, your app is not able to modify the settings that have
112moved to {@link android.provider.Settings.Global} when running on Android 4.2 and higher.</p>
113 </li>
Scott Main488c81a2013-02-20 15:32:30 -0800114
115 <li>If your app uses {@link android.webkit.WebView}, Android 4.2 adds an additional layer of
116 security so you can more safely <b>bind JavaScript to your
117 Android code</b>. If you set your
118 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
119 to 17 or higher, you must now add the {@code @JavascriptInterface} annotation to any method that you
120 want available to your JavaScript (the method must also be public). If you do not provide the
121 annotation, the method is not accessible by a web page in your {@link android.webkit.WebView}
122 when running on Android 4.2 or higher. If you set the
123 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
124 to 16 or lower, the annotation is not required, but we recommend that you update your target version
125 and add the annotation for additional security.
126 <p>Read more about <a href="{@docRoot}guide/webapps/webview.html#BindingJavaScript">binding
127 JavaScript code to Android code</a>.</p></li>
Scott Main57655252012-11-13 00:44:17 -0800128</ul>
129
130
131
132
133
134
Scott Mainca211c72012-11-13 09:08:42 -0800135<h2 id="Daydream">Daydream</h2>
Scott Main57655252012-11-13 00:44:17 -0800136
137<p>Daydream is a new interactive screensaver mode for Android devices. It activates automatically
138when the device is inserted into a dock or when the device is left idle while plugged in to a
139charger (instead of turning the screen off). Daydream displays one dream at a time, which may
Scott Main66e642b2012-11-13 11:24:35 -0800140be a purely visual, passive display that dismisses upon touch, or may be interactive and responsive
Scott Main57655252012-11-13 00:44:17 -0800141to the full suite of input events. Your dreams run in your app’s process and have full access to
142the Android UI toolkit, including views, layouts, and animations, so they are more flexible and
143powerful than either live wallpapers or app widgets.</p>
144
145<p>You can create a dream for Daydream by implementing a subclass of {@link
146android.service.dreams.DreamService}. The {@link android.service.dreams.DreamService} APIs are
147designed to be similar to those of {@link android.app.Activity}. To specify the UI for your
148dream, pass a layout resource ID or {@link android.view.View} to {@link
149android.service.dreams.DreamService#setContentView setContentView()} at any point after you have
Scott Main66e642b2012-11-13 11:24:35 -0800150a window, such as from the {@link android.service.dreams.DreamService#onAttachedToWindow()}
151callback.</p>
Scott Main57655252012-11-13 00:44:17 -0800152
153<p>The {@link android.service.dreams.DreamService} class provides other important lifecycle callback
154methods on top of the base {@link android.app.Service} APIs, such as {@link
155android.service.dreams.DreamService#onDreamingStarted()}, {@link
156android.service.dreams.DreamService#onDreamingStopped()}, and {@link
Scott Main66e642b2012-11-13 11:24:35 -0800157android.service.dreams.DreamService#onDetachedFromWindow()}.
158You cannot initiate a {@link android.service.dreams.DreamService} from your
159app&mdash;it is launched automatically by the system.</p>
Scott Main57655252012-11-13 00:44:17 -0800160
161<p>If your dream is interactive, you can start an activity from the dream to send the user into
Scott Main66e642b2012-11-13 11:24:35 -0800162your app’s full UI for more detail or control. You can use {@link
Scott Main57655252012-11-13 00:44:17 -0800163android.service.dreams.DreamService#finish()} to end the dream so the user can see the
Scott Main66e642b2012-11-13 11:24:35 -0800164new Activity.</p>
Scott Main57655252012-11-13 00:44:17 -0800165
Scott Main66e642b2012-11-13 11:24:35 -0800166<p>To make your daydream available to the system, declare your {@link
Scott Main57655252012-11-13 00:44:17 -0800167android.service.dreams.DreamService} with a <a
Scott Main66e642b2012-11-13 11:24:35 -0800168href="{@docRoot}guide/topics/manifest/service-element.html">{@code &lt;service>}</a> element
169in your manifest file. You must then include an intent filter with the action {@code
Scott Main57655252012-11-13 00:44:17 -0800170"android.service.dreams.DreamService"}. For example:</p>
171
172<pre>
173&lt;service android:name=".MyDream" android:exported="true"
174 android:icon="@drawable/dream_icon" android:label="@string/dream_label" >
175 &lt;intent-filter>
176 &lt;action android:name="android.service.dreams.DreamService" />
177 &lt;category android:name="android.intent.category.DEFAULT" />
178 &lt;/intent-filter>
179&lt;/service>
180</pre>
181
182<p>There are some other useful methods in {@link android.service.dreams.DreamService}
183to be aware of:</p>
184
185<ul>
186 <li>{@link android.service.dreams.DreamService#setInteractive(boolean)} controls whether
Scott Main66e642b2012-11-13 11:24:35 -0800187the dream receives input events or exits immediately upon user input. If the dream is
188interactive, the user may use the <em>Back</em> or <em>Home</em> buttons to exit the dream or you can call
Scott Main57655252012-11-13 00:44:17 -0800189{@link android.service.dreams.DreamService#finish()} to stop the dream.</li>
Scott Main66e642b2012-11-13 11:24:35 -0800190 <li>If you want a fully immersive display, you can call {@link
Scott Main57655252012-11-13 00:44:17 -0800191android.service.dreams.DreamService#setFullscreen
Scott Main66e642b2012-11-13 11:24:35 -0800192setFullscreen()} to hide the status bar.</li>
Scott Main57655252012-11-13 00:44:17 -0800193 <li>Before Daydream starts, the display dims to signal to the user that the idle timeout
194is approaching. Calling {@link android.service.dreams.DreamService#setScreenBright
195setScreenBright(true)} allows you to instead set the display at its usual brightness.</li>
196</ul>
197
198<p>For more information, see the {@link android.service.dreams.DreamService} documentation.</p>
199
200
201
202
203
204
205
206
207
208
209
210<h2 id="SecondaryDisplays">Secondary Displays</h2>
211
212<p>Android now allows your app to display unique content on additional screens that are connected
Scott Main66e642b2012-11-13 11:24:35 -0800213to the user’s device over either a wired connection or Wi-Fi.
214 To create unique content for a secondary display, extend the {@link android.app.Presentation}
Scott Main57655252012-11-13 00:44:17 -0800215class and implement the {@link android.app.Presentation#onCreate onCreate()} callback. Within
216{@link android.app.Presentation#onCreate onCreate()}, specify your UI for the secondary display
Scott Main66e642b2012-11-13 11:24:35 -0800217by calling {@link android.app.Presentation#setContentView setContentView()}.
218As an extension of the {@link android.app.Dialog} class, the {@link
219android.app.Presentation} class provides the region in which your app can display a unique UI on the
220secondary display.</p>
Scott Main57655252012-11-13 00:44:17 -0800221
222<p>To detect secondary displays where you can display your {@link android.app.Presentation},
223use either the {@link android.hardware.display.DisplayManager} or {@link android.media.MediaRouter}
224APIs. While the {@link android.hardware.display.DisplayManager} APIs allow you to enumerate
225multiple displays that may be connected at once, you should usually use {@link
226android.media.MediaRouter} instead to quickly access the system’s default display for
227presentations.</p>
228
Scott Main66e642b2012-11-13 11:24:35 -0800229<p>To get the default display for your presentation, call {@link
Scott Main57655252012-11-13 00:44:17 -0800230android.media.MediaRouter#getSelectedRoute MediaRouter.getSelectedRoute()} and pass it
231{@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO}. This returns a {@link
232android.media.MediaRouter.RouteInfo} object that describes the system’s currently selected route
233for video presentations. If the {@link android.media.MediaRouter.RouteInfo} is not null, call
234{@link android.media.MediaRouter.RouteInfo#getPresentationDisplay()} to get the {@link
235android.view.Display} representing the connected display.</p>
236
237<p>You can then display your presentation by passing the {@link android.view.Display} object
238to a constructor for your {@link android.app.Presentation} class. Your presentation will now
239appear on the secondary display.</p>
240
241<p>To detect at runtime when a new display has been connected, create an instance of {@link
242android.media.MediaRouter.SimpleCallback} in which you implement the {@link
243android.media.MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged
244onRoutePresentationDisplayChanged()} callback method, which the system will call when a new
245presentation display is connected. Then register the {@link
246android.media.MediaRouter.SimpleCallback} by passing it to {@link
247android.media.MediaRouter#addCallback MediaRouter.addCallback()} along with the {@link
248android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO} route type. When you receive a call to
249{@link android.media.MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged
250onRoutePresentationDisplayChanged()}, simply call {@link
251android.media.MediaRouter#getSelectedRoute MediaRouter.getSelectedRoute()} as mentioned above.</p>
252
253<p>To further optimize the UI in your {@link android.app.Presentation} for
254secondary screens, you can apply
255a different theme by specifying the {@link
256android.R.attr#presentationTheme android:presentationTheme} attribute in the <a
Scott Main188315c2013-01-03 18:02:21 -0800257href="{@docRoot}guide/topics/resources/style-resource.html">{@code &lt;style>}</a> that you’ve
Scott Main57655252012-11-13 00:44:17 -0800258applied to your application or activity.</p>
259
260<p>Keep in mind that screens connected to the user’s device often have a larger screen size and
261likely a different screen density. Because the screen characteristics may different, you should
262provide resources that are optimized specifically for such larger displays. If you need
263to request additional resources from your {@link
264android.app.Presentation}, call {@link android.app.Presentation#getContext()}{@link
265android.content.Context#getResources .getResources()} to get the {@link
266android.content.res.Resources} object corresponding to the display. This provides
267the appropriate resources from your app that are best suited for the
268secondary display's screen size and density.</p>
269
270<p>For more information and some code samples, see the {@link android.app.Presentation}
271class documentation.</p>
272
273
274
275
276
277
278
279
280
281
282<h2 id="Lockscreen">Lockscreen Widgets</h2>
283
284<p>Android now allows users to add app widgets to the lock screen. To make your <a
285href="{@docRoot}guide/topics/appwidgets/index.html">App Widget</a> available for use on the
286lock screen, add the {@link android.appwidget.AppWidgetProviderInfo#widgetCategory
287android:widgetCategory} attribute to your XML file that specifies the {@link
288android.appwidget.AppWidgetProviderInfo}. This attribute supports two values: {@code home_screen}
289and {@code keyguard}. By default, the attribute is set to {@code home_screen} so users can add your
290app widget to the Home screen. If you want your app widget to be also available on the lock
291screen, add the {@code keyguard} value:</p>
292
293<pre>
294&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
295 ...
296 android:widgetCategory="keyguard|home_screen">
297&lt;/appwidget-provider>
298</pre>
299
300<p>You should also specify an initial layout for your app widget when on the lock screen with
301the {@link android.appwidget.AppWidgetProviderInfo#initialKeyguardLayout
302android:initialKeyguardLayout} attribute. This works the same way as the {@link
303android.appwidget.AppWidgetProviderInfo#initialLayout android:initialLayout}, in that it provides
304a layout that can appear immediately until your app widget is initialized and able to update the
305layout.</p>
306
307<p>For more information about building app widgets for the lock screen, including to properly
308size your app widget when on the lock screen, see the <a
309href="{@docRoot}guide/topics/appwidgets/index.html#lockscreen">App Widgets</a> guide.</p>
310
311
312
313
314
315
316
317<h2 id="MultipleUsers">Multiple Users</h2>
318
319<p>Android now allows multiple user spaces on shareable devices such as tablets. Each user on a
320device has his or her own set of accounts, apps, system settings, files, and any other
321user-associated data.</p>
322
323<p>As an app developer, there’s nothing different you need to do in order for your app to work
324properly with multiple users on a single device. Regardless of how many users may exist on a
325device, the data your app saves for a given user is kept separate from the data your app saves
326for other users. The system keeps track of which user data belongs to the user process in which
327your app is running and provides your app access to only that user’s data and does not allow
328access to other users’ data.</p>
329
330<h3>Saving data in a multi-user environment</h3>
331
332<p>Whenever your app saves user preferences, creates a database, or writes a file to the user’s
333internal or external storage space, that data is accessible only while running as that user.</p>
334
335<p>To be certain that your app behaves properly in a multi-user environment, do not refer to your
336internal app directory or external storage location using hard-coded paths and instead always use
337the appropriate APIs:</p>
338<ul>
339 <li>For access to internal storage, use {@link android.content.Context#getFilesDir()}, {@link
340android.content.Context#getCacheDir()}, or {@link android.content.Context#openFileOutput
341openFileOutput()}.</li>
342 <li>For access to external storage, use {@link android.content.Context#getExternalFilesDir
343 getExternalFilesDir()} or {@link android.os.Environment#getExternalStoragePublicDirectory
344 getExternalStoragePublicDirectory()}.
345</ul>
346
347<p>No matter which of these APIs you use to save data for a given user, the data will not be
348accessible while running as a different user. From your app’s point of view, each user is running
349on a completely separate device.</p>
350
351<h3>Identifying users in a multi-user environment</h3>
352
353<p>If your app wants to identify unique users such as to gather analytics or create other account
354associations, you should follow the recommended practices for <a
355href="http://android-developers.blogspot.com/2011/03/identifying-app-installations.html">identifying
356unique installations</a>. By creating a new {@link java.util.UUID} when your app starts for the
357first time, you’re certain to obtain a unique ID for tracking each user, regardless of how many
358users install your app on a single device. Alternatively, you can save a local token fetched from
359your server or use the registrations ID provided by <a
Dirk Dougherty2b286bb2012-11-29 17:25:09 -0800360href="{@docRoot}google/gcm/index.html">Google Cloud Messaging</a>.</p>
Scott Main57655252012-11-13 00:44:17 -0800361
362<p>Beware that if your app requests one of the hardware device identifiers (such as the WiFi MAC
363address, the {@link android.os.Build#SERIAL} number, or the {@link
364android.provider.Settings.Secure#ANDROID_ID} number), they will provide the same value for each
365user because these identifiers are tied to the hardware and not the user. Not to mention the other
366problems these identifiers introduce as discussed in the <a
367href="http://android-developers.blogspot.com/2011/03/identifying-app-installations.html">Identifying
368App Installations</a> blog post.</p>
369
370<h3>New Global Settings</h3>
371
372<p>The system settings have been updated to support multiple users with the addition of {@link
373android.provider.Settings.Global}. This collection of settings is similar to {@link
374android.provider.Settings.Secure} settings because they are read-only, but applies globally across
375all user spaces on the device.</p>
376
377<p>Several existing settings were relocated here from either {@link
378android.provider.Settings.System} or {@link android.provider.Settings.Secure}. If your app is
379currently making changes to settings previously defined in {@link android.provider.Settings.System}
380(such as {@link android.provider.Settings.System#AIRPLANE_MODE_ON}), then you should expect that
381doing so will no longer work on a device running Android 4.2 or higher if those settings were
382moved to {@link android.provider.Settings.Global}. You can continue to read settings that are in
383{@link android.provider.Settings.Global}, but because the settings are no longer considered safe
384for apps to change, attempting to do so will fail silently and the system will write a warning to
385the system log when running your app on Android 4.2 or higher.</p>
386
387
388
389
390
391
392
393
394
395<h2 id="RTL">RTL Layout Support</h2>
396
397<p>Android now offers several APIs that allow you to build user interfaces that gracefully
398transform layout orientation to support languages that use right-to-left (RTL) UIs and reading
399direction, such as Arabic and Hebrew.</p>
400
401<p>To begin supporting RTL layouts in your app, set the {@link android.R.attr#supportsRtl
402android:supportsRtl} attribute to the {@code &lt;application>} element in your manifest file
403and set it {@code “true"}. Once you enable this, the system will enable various RTL APIs to
404display your app with RTL layouts. For instance, the action bar will show the icon and title
405on the right side and action buttons on the left, and any layouts you’ve created with the
406framework-provided {@link android.view.View} classes will also be reversed.</p>
407
408<p>If you need to further optimize the appearance of your app when displayed with an RTL layout,
409there are two basic levels of optimization:</p>
410
411<ol>
412 <li>Convert left- and right-oriented layout properties to start- and end-oriented layout
413properties.
414 <p>For example, use {@link android.R.attr#layout_marginStart android:layout_marginStart}
415in place of {@code android:layout_marginLeft} and {@link android.R.attr#layout_marginEnd
416android:layout_marginEnd} in place of {@code android:layout_marginRight}.
417 <p>The {@link android.widget.RelativeLayout} class also provides the corresponding layout
418attributes to replace left/right positions, such as {@code android:layout_alignParentStart} to
419replace {@code android:layout_alignParentLeft} and {@code android:layout_toStartOf} instead of
420{@code android:layout_toLeftOf}.
421 </li>
422 <li>Or to provide complete optimization for RTL layouts, you can provide entirely separate
423layout files using the {@code ldrtl} resource qualifier ({@code ldrtl} stands for
424layout-direction-right-to-left}). For example, you can save your default layout files in
425{@code res/layout/} and your RTL optimized layouts in {@code res/layout-ldrtl/}.
426 <p>The {@code ldrtl} qualifier is great for drawable resources, so that you can provide
427graphics that are oriented in the direction corresponding to the reading direction.</p>
428 </li>
429</ol>
430
431<p>Various other APIs are available across the framework to support RTL layouts, such as in
432the {@link android.view.View} class so that you can implement the proper behaviors for custom
433views and in {@link android.content.res.Configuration} to query the current layout direction.</p>
434
435<p><strong>Note:</strong> If you are using SQlite and have tables or column names that are
436“number only," be
437careful: using <a href="{@docRoot}reference/java/lang/String.html#format(String, Object...)">{@code
438String.format(String, Object...)}</a> can lead to errors where the numbers
439have been converted to their Arabic equivalents if your device has been set to the Arabic locale.
440You must use <a href="{@docRoot}reference/java/lang/String.html#format(Locale,String,Object...)">{@code
441String.format(Locale,String,Object...)}</a> to ensure numbers are
442preserved as ASCII. Also use <a href="{@docRoot}reference/java/lang/String.html#format(String,int)">{@code
443String.format("%d", int)}</a> instead of using
444<a href="{@docRoot}reference/java/lang/String.html#valueOf(int)">{@code String.valueOf(int)}</a> for
445formatting numbers.</p>
446
447
448
449
450
451
452
453
454
455
456
457
458
459<h2 id="NestedFragments">Nested Fragments</h2>
460
461<p>You can now embed fragments inside fragments. This is useful for a variety of situations in
462which you want to place dynamic and re-usable UI components into a UI component that is itself
463dynamic and re-usable. For example, if you use {@link android.support.v4.view.ViewPager} to
464create fragments that swipe left and right and consume a majority of the screen space, you can
465now insert fragments into each fragment page.</p>
466
467<p>To nest a fragment, simply call {@link android.app.Fragment#getChildFragmentManager()} on
468the {@link android.app.Fragment} in which you want to add a fragment. This returns a {@link
469android.app.FragmentManager} that you can use like you normally do from the top-level activity
470to create fragment transactions. For example, here’s some code that adds a fragment from within
471an existing {@link android.app.Fragment} class:</p>
472
473<pre>
474Fragment videoFragment = new VideoPlayerFragment();
475FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
476transaction.add(R.id.video_fragment, videoFragment).commit();
477</pre>
478
479<p>From within a nested fragment, you can get a reference to the parent fragment by calling
480{@link android.app.Fragment#getParentFragment()}.</p>
481
482<p>The Android Support Library also now supports nested fragments, so you can implement nested
483fragment designs on Android 1.6 and higher.</p>
484
485<p><strong>Note:</strong> You cannot inflate a layout into a fragment when that layout
486includes a {@code &lt;fragment>}. Nested fragments are only supported when added to a
487fragment dynamically.</p>
488
489
490
491
492
493
494<h2 id="Renderscript">Renderscript</h2>
495
496<p>Renderscript computation functionality has been enhanced with the following features:</p>
497<dl>
498 <dt><b>Script intrinsics</b></dt>
499 <dd><p>You can use Renderscript's built-in script intrinsics that implement
500common operations for you such as:</p>
501 <ul>
502 <li>{@link android.renderscript.ScriptIntrinsicBlend Blends}</li>
503 <li>{@link android.renderscript.ScriptIntrinsicBlur Blur}</li>
504 <li>{@link android.renderscript.ScriptIntrinsicColorMatrix Color matrix}</li>
505 <li>{@link android.renderscript.ScriptIntrinsicConvolve3x3 3x3 convolve}</li>
506 <li>{@link android.renderscript.ScriptIntrinsicConvolve5x5 5x5 convolve}</li>
507 <li>{@link android.renderscript.ScriptIntrinsicLUT Per-channel lookup table}</li>
508 <li>{@link android.renderscript.ScriptIntrinsicYuvToRGB Converting an Android YUV buffer to RGB}</li>
509 </ul>
510 <p>To use a script intrinsic, call the static <code>create()</code> method of each instrinsic
511 to create an instance of the script. You then call the available <code>set()</code>
512 methods of each script intrinsic to set any necessary inputs and options.
513 Finally, call the {@link android.renderscript.ScriptC#forEach forEach()}</code>
514 method to execute the script.</p>
515 </dd>
516
517
518<dt><b>Script Groups</b></dt>
519<dd>
520<p>{@link android.renderscript.ScriptGroup}s allow you to chain together related Renderscript
521scripts and execute them with one call.</p>
522
523<p>Use a {@link android.renderscript.ScriptGroup.Builder} to add all of the scripts to the group
524by calling {@link android.renderscript.ScriptGroup.Builder#addKernel addKernel()}. Once you
525add all the scripts, create the connections between the
526scripts by calling {@link android.renderscript.ScriptGroup.Builder#addConnection addConnection()}.
527When you are done adding the connections, call {@link android.renderscript.ScriptGroup.Builder#create create()}
528to create the script group. Before executing the script group, specify the input
529{@link android.renderscript.Allocation} and initial script to run with the
530{@link android.renderscript.ScriptGroup#setInput} method and provide the output
531{@link android.renderscript.Allocation} where the result will be written to and final script to
532run with {@link android.renderscript.ScriptGroup#setOutput setOutput()}. Finally, call
533{@link android.renderscript.ScriptGroup#execute execute()} to run the script group.
534</p>
535</dd>
536
537<dt><b>Filterscript</b></dt>
538<dd>
539<p>Filterscript defines constraints on the existing Renderscript APIs that allow the resulting code to run
540on a wider variety of processors (CPUs, GPUs, and DSPs). To create Filterscript files, create <code>.fs</code>
541files instead of <code>.rs</code> files, and specify <code>#pragma rs_fp_relaxed</code> to
542tell the Renderscript runtime your scripts do not require strict IEEE 754-2008 floating point precision.
543This precision allows flush-to-zero for denorms and round-towards-zero. In addition, your Filterscript
544scripts must not use 32-bit built-in types and must specify a custom root function by using the
545<code>__attribute__((kernel))</code> attribute because Filterscript does not support pointers, which
546the default signature of the <code>root()</code> function defines.</p>
547</dd>
548
549</dl>
550
Scott Mainca211c72012-11-13 09:08:42 -0800551<p class="note"><strong>Note:</strong> Although Filterscript support is in the platform, developer
552support will be available in ADT and SDK Tools Release 21.0.1. </p>
Scott Main57655252012-11-13 00:44:17 -0800553
554
555<p>For a detailed view of all API changes in Android 4.2, see the
556<a href="{@docRoot}sdk/api_diff/17/changes.html">API Differences Report</a>.</p>
557
558
559