Back button works like home

后端 未结 4 399
挽巷
挽巷 2021-01-17 05:11

Is it possible to change this code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    }
             


        
相关标签:
4条回答
  • 2021-01-17 05:36

    try this,

    @Override
        public void onBackPressed() {
            Intent backtoHome = new Intent(Intent.ACTION_MAIN);
            backtoHome.addCategory(Intent.CATEGORY_HOME);
            backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(backtoHome);
        }
    

    Adding this to your Activity, will make it look like your app is responding to a Home Button Click event

    0 讨论(0)
  • 2021-01-17 05:45

    Going to home screen programmatically

    to launch home screen

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    

    Note :

    This Intent will start the launcher application that the user has defined. Be careful with this because this will look like your application crashed if the user does not expect this.

    If you want this to build an exit button from your app please read this article on exit Buttons in Android

    0 讨论(0)
  • 2021-01-17 05:51

    One line solution

    @Override
    public void onBackPressed() {
       moveTaskToBack(true);
    }
    

    And also see this

    0 讨论(0)
  • 2021-01-17 05:56
    @Override
    public void onBackPressed()
        //super.onBackPressed();
        NavUtils.navigateUpFromSameTask(this);
    }
    

    If something doesn't work like expected try it with uncommented

        super.onBackPressed();
    
    0 讨论(0)
提交回复
热议问题