you can just remove this onBackPressed
as android implicitly finishes your activity on back key pressed.
And don't call addToBackStack
:
ft.addToBackStack(null);
remove the above line this adds your fragment transaction in backstack and on back key pressed first transaction is reverted and then activity is finished.
If you remove this back press will finish the activity.
change to:
if(hasFocus){
frame.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
fragment = new ViewDetail();
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
ft.addToBackStack(null);
ft.replace(R.id.container, fragment, "Dettaglio");
ft.commit();//the error is here
}
}, 500);
}