Override back button to act like home button

后端 未结 10 584
小鲜肉
小鲜肉 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 08:52

    try to override void onBackPressed() defined in android.app.Activity class.

    0 讨论(0)
  • 2020-11-22 08:59

    I've tried all the above solutions, but none of them worked for me. The following code helped me, when trying to return to MainActivity in a way that onCreate gets called:

    Intent.FLAG_ACTIVITY_CLEAR_TOP is the key.

      @Override
      public void onBackPressed() {
          Intent intent = new Intent(this, MainActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
      }
    
    0 讨论(0)
  • 2020-11-22 09:00

    If you want to catch the Back Button have a look at this post on the Android Developer Blog. It covers the easier way to do this in Android 2.0 and the best way to do this for an application that runs on 1.x and 2.0.

    However, if your Activity is Stopped it still may be killed depending on memory availability on the device. If you want a process to run with no UI you should create a Service. The documentation says the following about Services:

    A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.

    These seems appropriate for your requirements.

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

    Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading.) If that's the case, then your Activity can finish as usual and the Service will still be running.

    A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows:

    // 2.0 and above
    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
    
    // Before 2.0
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion.

    NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

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

    Override onBackPressed() after android 2.0. Such as

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
    
    0 讨论(0)
  • 2020-11-22 09:05

    if it helps someone else, I had an activity with 2 layouts that I toggled on and off for visibilty, trying to emulate a kind of page1 > page2 structure. if they were on page 2 and pressed the back button I wanted them to go back to page 1, if they pressed the back button on page 1 it should still work as normal. Its pretty basic but it works

    @Override
    public void onBackPressed() {
    // check if page 2 is open
        RelativeLayout page2layout = (RelativeLayout)findViewById(R.id.page2layout);
        if(page2layout.getVisibility() == View.VISIBLE){
            togglePageLayout(); // my method to toggle the views
            return;
        }else{
            super.onBackPressed(); // allows standard use of backbutton for page 1
        }
    
    }
    

    hope it helps someone, cheers

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