I think I have found the best solution for this!
First, execute the fragment transaction as follows:
new Handler().postDelayed(new Runnable() { @Override public void run() { getSupportFragmentManager() .beginTransaction() .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out) .replace(R.id.container, finalMenuFragment) .commit(); } }, 0);
I know it is useless to do postDelayed with 0, but this hack avoids the lag in the animation of clicks on the box, especially when you use android:background="?attr/selectableItemBackground" for an interactive element.
And then you close the box at the end of the onResume () function of your fragment (in this example, "finalMenuFragment") as follows:
new Handler().postDelayed(new Runnable() { public void run() { mDrawerLayout.closeDrawer(mFragmentContainerView); } }, 0);
Again I know it seems silly postDelayed with 0, but it makes smooth animation smooth.
Thus, the box will close as quickly as possible, if you need smooth animations, and if your fragment does not have many views, it will close faster and without delay.
I would love to get some feedback on this if anyone has verified this solution, hope this helps!
Quentin G. Sep 08 '15 at 10:58 2015-09-08 10:58
source share