I've implemented my fragment in this way: every time a user click on back button, it goes this function:
public void onResume()
{
super.onResume();
if(getView() == null)
{
return;
}
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
{
Fragment fragment = new FlightPerformanceFragment();
manager.beginTransaction().replace(R.id.content_main,fragment).commit();
Log.e("resume","resume FlightPerformance");
return true;
}
return false;
}
});
}
this works good if only there aren't editText focused.
I've searched a lot and I try to solve this problem in this way, using OnKeyListener
:
View.OnKeyListener onKeyListener = new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
//check if the right key was pressed
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if(v == tasET)
tasET.clearFocus();
else if(v == rhoET)
rhoET.clearFocus();
return true;
}
}
return false;
}
};
tasET = (EditText) rootView.findViewById(R.id.paisaET);
tasET.setOnKeyListener(onKeyListener);
the problem is that sometimes the method clearFocus() turn back on the previous editText and it not clear the focus at every editText.
So if this method seems a bit complicate to do simple back operation (I couldn't use onBackPressed() because I need to replace other fragments), is there also a simplest way to do it?
Thank you for help.