1page.title=Exiting Full-Screen Activities 2 3@jd:body 4 5<div id="tb-wrapper"> 6<div id="tb"> 7<h2>This lesson teaches you to</h2> 8<ol> 9 <li><a href="#disable-swipe">Disable the Swipe-To-Dismiss Gesture</a></li> 10 <li><a href="#long-press">Implement the Long Press to Dismiss Pattern</a></li> 11</ol> 12<h2>You should also read</h2> 13<ul> 14 <li><a href="{@docRoot}design/wear/index.html">Android Wear Design Principles</a></li> 15</ul> 16</div> 17</div> 18 19 20<p>By default, users exit Android Wear activities by swiping from left to right. If the app 21contains horizontally scrollable content, users first have to navigate to the edge of the 22content and then swipe again from left to right to exit the app.</p> 23 24<p>For more immersive experiences, like an app that can scroll a map in any direction, you can 25disable the swipe to exit gesture in your app. However, if you disable it, you must implement 26the long-press-to-dismiss UI pattern to let users exit your app using the 27<code>DismissOverlayView</code> class from the Wearable UI Library. 28You must also inform your users the first time they run your app that they can exit using 29a long press.</p> 30 31<p>For design guidelines about exiting Android Wear activities, see 32<a href="{@docRoot}design/wear/structure.html#Custom">Breaking out of the card</a>.</p> 33 34 35<h2 id="disable-swipe">Disable the Swipe-To-Dismiss Gesture</h2> 36 37<p>If the user interaction model of your app interferes with the swipe-to-dismiss gesture, 38you can disable it for your app. To disable the swipe-to-dismiss gesture in your app, extend 39the default theme and set the <code>android:windowSwipeToDismiss</code> attribute to 40<code>false</code>:</p> 41 42<pre> 43<style name="AppTheme" parent="Theme.DeviceDefault"> 44 <item name="android:windowSwipeToDismiss">false</item> 45</style> 46</pre> 47 48<p>If you disable this gesture, you must implement the long-press-to-dismiss UI pattern to let users 49exit your app, as described in the next section.</p> 50 51 52<h2 id="long-press">Implement the Long Press to Dismiss Pattern</h2> 53 54<p>To use the <code>DissmissOverlayView</code> class in your activity, add this element to 55your layout definition such that it covers the whole screen and is placed above all other views. 56For example:</p> 57 58<pre> 59<FrameLayout 60 xmlns:android="http://schemas.android.com/apk/res/android" 61 android:layout_height="match_parent" 62 android:layout_width="match_parent"> 63 64 <!-- other views go here --> 65 66 <android.support.wearable.view.DismissOverlayView 67 android:id="@+id/dismiss_overlay" 68 android:layout_height="match_parent" 69 android:layout_width="match_parent"/> 70<FrameLayout> 71</pre> 72 73<p>In your activity, obtain the <code>DismissOverlayView</code> element and set some introductory 74text. This text is shown to users the first time they run your app to inform them that they 75can exit the app using a long press gesture. Then use a <code>GestureDetector</code> to detect 76a long press:</p> 77 78<pre> 79public class WearActivity extends Activity { 80 81 private DismissOverlayView mDismissOverlay; 82 private GestureDetector mDetector; 83 84 public void onCreate(Bundle savedState) { 85 super.onCreate(savedState); 86 setContentView(R.layout.wear_activity); 87 88 // Obtain the DismissOverlayView element 89 mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay); 90 mDismissOverlay.setIntroText(R.string.long_press_intro); 91 mDismissOverlay.showIntroIfNecessary(); 92 93 // Configure a gesture detector 94 mDetector = new GestureDetector(this, new SimpleOnGestureListener() { 95 public void onLongPress(MotionEvent ev) { 96 mDismissOverlay.show(); 97 } 98 }); 99 } 100 101 // Capture long presses 102 @Override 103 public boolean onTouchEvent(MotionEvent ev) { 104 return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev); 105 } 106} 107</pre> 108 109<p>When the system detects a long press gesture, <code>DismissOverlayView</code> shows an 110<strong>Exit</strong> button, which terminates your activity if the user presses it.</p>