I am trying to implement back navigation through my fragments which are called in specific order: A
->B
->C
and by going back with hardware button I would like them to remain order.
I am using fragmenttransaction.replace
in order to switch fragment with no addToBackStack
because it made my ActionBarMenu
to misbehave.
Problem is that when I am on fragment C
back button is going back directly to A
. I found out that it is because click
event is executed twice I am going to B
and directly to A
.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setOnBackButtonPresed();
}
private void setOnBackButtonPresed() {
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener( new View.OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
goBackToDays();
return false;
}
return false;
}
} );
}
private void goBackToDays() {
Log.e("fragmentC", "executing on back action")
}
logcat result:
03-15 08:37:17.353 21245-21245/com.test E/fragmentC: executing on back action
03-15 08:37:17.390 21245-21245/com.test E/fragmentC: executing on back action
Can anyone give me a hint how I can avoide twice button events?