How to detect if the Up button was pressed
Asked Answered
M

2

12

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

When I press the left arrow the activity goes back to the previous activity, but no event is registered in the onKeyUp, OnkeyDown and OnBackPressed methods.

But when I press the Back key on the phone (at the bottom) the activity goes back to the previous one and all the methods onKeyUp, OnKeyDown and OnBackPressed register an event (in the logcat).

How can I capture that 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 activity is destroyed by the user and not by the system (for example, if the user switches to another activity).

By further investigating he matter I found out that the UP button gives an event that is captured by the onOptionsItemSelected method and since there is no other button on the menu I know it is this button.

Mayonnaise answered 6/8, 2015 at 23:15 Comment(1)
Thx. I added the code to test for the home key, in case I will add other action items to the action bar in the futureMayonnaise
P
12

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

Handling clicks on action items

When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using the MenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the tag's id attribute so you can perform the appropriate action. For example:

@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, via the Fragment class's onCreateOptionsMenu() callback, the system calls onOptionsItemSelected() for that fragment when the user selects one of those items. However, the activity gets a chance to handle the event first, so the system first calls onOptionsItemSelected() on the activity, before calling the same callback for the fragment. To ensure that any fragments in the activity also have a chance to handle the callback, always pass the call to the superclass as the default behavior instead of returning false when you do not handle the item.

To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    ... 
} 
Pothole answered 6/8, 2015 at 23:25 Comment(1)
Thank You! Even though this thread is pretty old, this was the one that worked! (as I faced the same problem). ThanksCelle
G
4

Yes you are right, you can detect if 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); 
}
Grano answered 6/8, 2015 at 23:24 Comment(1)
Awesome! Thanks!Farthermost

© 2022 - 2024 — McMap. All rights reserved.