2

i have an activity with a viewpager and 4 fragments as pages.

inside one of the fragment i need to handle back button press, so i do that this way:

public void setBackButtonAction(final View view)
{
    //view is root view of fragment
    view.setFocusableInTouchMode(true);
    view.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
            {
                if (currentView == VIEW_MODE_ALL_CAT)
                {
                    return false;
                }
                else if (currentView == VIEW_MODE_THIS_CAT)
                {
                    currentView = VIEW_MODE_ALL_CAT;
                    // do something
                    return true;
                }
            }

            return false;
        }
    });
}

so far so good, this works as expected.

and i have a EditText inside this fragment, and i need to connect it to an adapter for filtering result, i do that this way:

public void setSearchFilterListener()
{
    searchFilter.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            mAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s)
        {
        }
    });
}

this works also as expected.

BUT if i use this EditText, and then pressing the back button, my code for handling back button wont call at all.

what just happened? if i open the app and press the back button the code for handling back is being called, But if i use EditText and then pressing the back Button, the code for handling back wont call at all.

anybody know whats going on here?

any help would be appreciated.

mehdok
  • 1,499
  • 4
  • 29
  • 54

1 Answers1

1

Because with your method setBackButtonAction, you have only set the onKeyListener on the root view, when your EditText is set as focus, it will find the onKeyListener of the EditText, but not the onKeyListener of the root view.

To probably override back behaviour, you should override onBackPressed()

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • i can not use `onBackPressed()`, beacause i'm in the `Fragment` not `Activity`, and every back button in every fragments handle differently. But based on your answer i set the `keyListener` for editText also, and the problem solved. tnx. – mehdok Aug 30 '15 at 06:31
  • 1
    The best solution, create your own Fragment class with abstract method onBackPressed(), have your activity's `onBackPressed()` find the active fragment and call it. It needs more work, but it is best. – Derek Fung Aug 30 '15 at 06:33
  • this is really nice, but i have 4 fragments, so how do i know which `onBackPressed()` must be called? i know i must find the current showing fragment from the `viewpager`, but how? – mehdok Aug 30 '15 at 06:43
  • 1
    http://stackoverflow.com/questions/18609261/getting-the-current-fragment-instance-in-the-viewpager this may help – Derek Fung Aug 30 '15 at 06:48