Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride back button to act like home button
    primarykey
    data
    text
    <p>On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state.</p> <p>In the Android <a href="http://developer.android.com/intl/fr/guide/practices/ui_guidelines/activity_task_design.html" rel="noreferrer">docs</a> it states:</p> <blockquote> <p>...not all activities have the behavior that they are destroyed when BACK is pressed. When the user starts playing music in the Music application and then presses BACK, the application overrides the normal back behavior, preventing the player activity from being destroyed, and continues playing music, even though its activity is no longer visible</p> </blockquote> <p><strong>How do I replicate this functionality in my own application?</strong></p> <p>I think there must be three possibilities...</p> <ol> <li><p>Capture the back button press (as below) and then call whatever method(s) the home button calls. </p> <pre><code>@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { Log.d(this.getClass().getName(), "back button pressed"); } return super.onKeyDown(keyCode, event); } </code></pre></li> <li><p>Capture the back button press and then spoof a home button press. </p></li> <li><p>Capture the back button press, then start an Activity of the home screen, effectively putting my application's Activity into the stopped state.</p></li> </ol> <p><strong>Edit:</strong> I know about services and am using one in the application to which this problem is related. This question is specifically about putting the Activity into the stopped state rather than the destroyed state on pressing the back button.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload