Override back button to act like home button

后端 未结 10 585
小鲜肉
小鲜肉 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:12

    I have use @Mirko N. answser using made the new Custom EditText

     public class EditViewCustom extends EditText {
    
        Button cancelBtn;
        RelativeLayout titleReleLayout;
        public EditViewCustom(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public EditViewCustom(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public EditViewCustom(Context context) {
            super(context);
        }
    
        public void setViews(Button cancelBtn,RelativeLayout titleReleLayout){
            this.cancelBtn = cancelBtn;
            this.titleReleLayout = titleReleLayout;
        }
    
        @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Log.d("KEYCODE_BACK","KEYCODE_BACK");
                cancelBtn.setVisibility(View.GONE);
                this.setFocusableInTouchMode(false);
                this.setFocusable(false);
                titleReleLayout.setVisibility(View.VISIBLE);
    
                return super.onKeyPreIme(keyCode, event);
              }
    
            return super.onKeyPreIme(keyCode, event);
        }
    
    }
    

    Then set data from your activity

     searchEditView.setViews(cancelBtn, titleRelativeLayout);
    

    Thank you.

    0 讨论(0)
  • 2020-11-22 09:14

    Use the following code:

    public void onBackPressed() {    
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-11-22 09:14

    Even better, how about OnPause():

    Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().

    When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure toenter code here not do anything lengthy here.

    This callback is mostly used for saving any persistent state the activity is editing and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one.

    This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题