Android - How To Override the “Back” button so it doesn't Finish() my Activity?

后端 未结 10 1259
粉色の甜心
粉色の甜心 2020-11-22 08:07

I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar.

This is so that when the User presses home a

10条回答
  •  误落风尘
    2020-11-22 08:33

    Remove your key listener or return true when you have KEY_BACK.

    You just need the following to catch the back key (Make sure not to call super in onBackPressed()).

    Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification or else Android will kill your service if it needs to free memory.

    @Override
    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);
    }
    

提交回复
热议问题