I am having an issue with the toolbar and the back button. Here is the setup I have:
When I add a detail fragment, I animate the toolbar hamburger as outlined here. and this causes the hamburger to animate to an arrow.
Even in the comments section, a user mentions:
This works perfectly. Just set start=0 and end=1 to go from hamburger to arrow, and start=1 and end=0 for arrow to hamburger. One thing you'll have to keep track of is when the drawer is closed when the arrow is shown. At this point, the hamburger ends up being shown (because of the drawer's slide), which you'll have to correct.
But I cannot figure out how to get the back arrow to function properly. When I press the back arrow, the drawer opens and the detail fragment does not pop. How should I go about implementing this?
In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.
After several hours of searching and playing around, I was able to build a solution that delivered on each requirement. Sources: 1,2
detailFragmentActive = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(mToolbar);
...
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(detailFragmentActive) {
onBackPressed();
//if(displayBackAgain)
//return; //return after so you don't call syncState();
}else if (mDrawerLayou.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
mDrawerLayout.openDrawer(GravityCompat.START);
mDrawerToggle.syncState();
}
});
}
private void animateHamburger(boolean isArrow){
int start = 0, end = 1;
if(isArrow){
detailFragmentActive = false;
start = 1; end = 0;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
detailFragmentActive = true;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mDrawerToggle.onDrawerSlide(mDrawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}
@Override
public void onBackPressed() {
super.onBackPressed();
animateHamburger(true);
}
public void onFragmentChange(){
...
animateHamburger(false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With