Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - How To Override the "Back" button so it doesn't Finish() my Activity?
    primarykey
    data
    text
    <p>I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar.</p> <p>This is so that when the User presses home and the Activity gets pushed to the background they can get back to the Activity via the Notification.</p> <p>The problem arises when a User presses the back button, my Activity gets destroyed but the Notification remains as I want the user to be able to press back but still be able to get to the Activity via the Notification. But when a USER tries this I get Null Pointers as its trying to start a new activity rather than bringing back the old one.</p> <p>So essentially I want the Back button to act the exact same as the Home button and here is how I have tried so far:</p> <hr> <pre><code> @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (Integer.parseInt(android.os.Build.VERSION.SDK) &lt; 5 &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) { Log.d("CDA", "onKeyDown Called"); onBackPressed(); } return super.onKeyDown(keyCode, event); } public void onBackPressed() { Log.d("CDA", "onBackPressed Called"); Intent setIntent = new Intent(Intent.ACTION_MAIN); setIntent.addCategory(Intent.CATEGORY_HOME); setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(setIntent); return; } </code></pre> <hr> <p>However the above code still seems to allow my Activity to be destroyed, How can I stop my Activity from being destroyed when the back button is pressed?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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