Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the Up button was pressed

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.

like image 331
Zvi Avatar asked Aug 06 '15 23:08

Zvi


1 Answers

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);
    ... 
} 
like image 51
phdfong - Kenneth Fong Avatar answered Sep 30 '22 20:09

phdfong - Kenneth Fong