Override back button to act like home button

后端 未结 10 581
小鲜肉
小鲜肉 2020-11-22 08:38

On pressing the back button, I\'d like my application to go into the stopped state, rather than the destroyed state.

In the Android docs it states:

10条回答
  •  清酒与你
    2020-11-22 09:15

    Working example..

    Make sure don't call super.onBackPressed();

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

    In this way your Back Button act like Home button . It doesn't finishes your activity but take it to background

    Second way is to call moveTaskToBack(true); in onBackPressed and be sure to remove super.onBackPressed

提交回复
热议问题