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<a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>DismissOverlayView</code></a>
28class from the Wearable UI Library. You must also inform your users the first time they run your app
29that they can exit using a 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&lt;style name="AppTheme" parent="Theme.DeviceDefault">
44    &lt;item name="android:windowSwipeToDismiss">false&lt;/item>
45&lt;/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
55<a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>DismissOverlayView</code></a>
56class in your activity, add this element to your layout definition such that it covers the whole
57screen and is placed above all other views.</p>
58
59<p>The following example shows how to add the
60<a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>&lt;DismissOverlayView&gt;</code></a>
61element:</p>
62
63<pre>
64&lt;FrameLayout
65    xmlns:android="http://schemas.android.com/apk/res/android"
66    android:layout_height="match_parent"
67    android:layout_width="match_parent">
68
69    &lt;!-- other views go here -->
70
71    &lt;android.support.wearable.view.DismissOverlayView
72        android:id="@+id/dismiss_overlay"
73        android:layout_height="match_parent"
74        android:layout_width="match_parent"/>
75&lt;FrameLayout>
76</pre>
77
78<p>In your activity, obtain the
79<a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>&lt;DismissOverlayView&gt;</code></a>
80element and set some introductory text. This text is shown to users the first time they run your app
81to inform them that they can exit the app using a long press gesture. Then use a
82<a href="{@docRoot}reference/android/view/GestureDetector.html"><code>GestureDetector</code></a>
83to detect a long press:</p>
84
85<pre>
86public class WearActivity extends Activity {
87
88    private DismissOverlayView mDismissOverlay;
89    private GestureDetector mDetector;
90
91    public void onCreate(Bundle savedState) {
92        super.onCreate(savedState);
93        setContentView(R.layout.wear_activity);
94
95        // Obtain the DismissOverlayView element
96        mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
97        mDismissOverlay.setIntroText(R.string.long_press_intro);
98        mDismissOverlay.showIntroIfNecessary();
99
100        // Configure a gesture detector
101        mDetector = new GestureDetector(this, new SimpleOnGestureListener() {
102            public void onLongPress(MotionEvent ev) {
103                mDismissOverlay.show();
104            }
105        });
106    }
107
108    // Capture long presses
109    &#64;Override
110    public boolean onTouchEvent(MotionEvent ev) {
111        return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
112    }
113}
114</pre>
115
116<p>When the system detects a long press gesture, the
117<a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>&lt;DismissOverlayView&gt;</code></a>
118element shows an <strong>Exit</strong> button, which terminates your activity if the user presses
119it.</p>