How to determine if the Up button has been pressed

In my activity, the action bar shows only the left arrow and the activity title.

When I click the left arrow, the activity returns to the previous action, but no events are logged in the onKeyUp, OnkeyDown and OnBackPressed methods.

But when I press the back key on the phone (bottom), the activity returns to the previous one, and all the onKeyUp, OnKeyDown and OnBackPressed methods register the event (in the logarithm).

How can I grab this left arrow (I think it is called the UP button)?

The reason I need to capture the key is to know in the onPause method that the action is destroyed by the user, and not by the system (for example, if the user switches to another activity).

With further research, it matters, I found that the UP button gives an event that is captured by the onOptionsItemSelected method, and since there is no other button in the menu, I know that this button.

+4
source share
2 answers

see http://developer.android.com/guide/topics/ui/actionbar.html#Handling

Handling clicks on action items

When a user clicks an action, the system calls your onOptionsItemSelected () activity. Using the MenuItem passed to this method, you can define the action by calling getItemId (). This returns the unique identifier provided by the tag identifier attribute so that you can perform the corresponding action. For instance:

@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case android.R.id.home: onUpButtonPressed(); return true; case R.id.action_search: openSearch(); return true; case R.id.action_compose: composeMessage(); return true; default: return super.onOptionsItemSelected(item); } } 

Note. If you inflate menu items from a fragment, through the fragment the callback onCreateOptionsMenu () class calls the onOptionsItemSelected () system calls for this fragment when the user selects one of these items. However, the activity gets the opportunity to handle the event first, so the system first calls onOptionsItemSelected () on before calling the same callback for the fragment. To ensure that any fragments in this activity also have the ability to handle callbacks, always pass the call to the superclass by default instead of returning false when you are not processing the element.

To enable the application icon as the Up button, call setDisplayHomeAsUpEnabled (). For instance:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); ... } 
+3
source

Yes, you are right, you can determine whether the up button was pressed in the onOptionsItemSelected method. This should work:

 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // Do something here. This is the event fired when up button is pressed. return true; } return super.onOptionsItemSelected(item); } 
+1
source

All Articles