I'm having some problems consistently animating a 'refresh' icon in the ActionBar of my app.
I have a container FragmentActivity which swaps fragments in and out as the user navigates through the app (either from within the fragment itself or from a SlidingMenu option). So when the app first loads, my FragmentContainer adds FragA. From FragA the user can navigate to FragB which is then swapped in.
In the action bar I display a static 'refresh' icon. As each Fragment loads, I replace this with an animated 'spinner' icon. When the load completes, I revert to the original refresh icon.
Problem is, this animation only works for the original fragment (FragA, in this case). When the user navigates to FragB and selects the refresh icon, the refresh is triggered, but the animation never happens. Similarly, if the back button is pressed to return to FragA, this now follows the same pattern i.e. the refresh button does not animate when pressed.
Things to note...
- I'm using ActionBarSherlock and the SlidingMenu implementation at https://github.com/jfeinstein10/SlidingMenu. So the above activity is actually a
SlidingFragmentActivity
. - Both Fragments call
setHasOptionsMenu(true)
- I've debugged through this andonCreateOptionsMenu
is being correctly called for each. - The icons are being correctly displayed for both Fragments - the animation is just not happening when I navigate off the 'default' fragment.
- I see the same behaviour when using the SlidingMenu to navigate - FragA loads, animation works -> SlidingMenu is used to navigate to a different fragment... animation doesn't work -> Back button to FragA... animation doesn't work here either.
- I'm using
FragmentTransaction.remove()
andadd()
rather thanreplace()
as I have previously had back-button issues withreplace()
- I am using the compatibility lib and I read on here that thereplace
implementation is a bit buggy - and not using it certainly fixed the issues I was seeing.
Code snippets below:
My code to load the original fragment is....
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, new FragA());
ft.addToBackStack(null);
ft.commit();
To 'swap' FragB for FragA....
public void switchContent(PysoBaseFragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(existingFragment);
ft.add(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.commit();
}
This method is declared in the container activity and is called from FragA i.e....
getFragmentContainer().switchContent(new FragB());
The code to spin the icon is called from the new Fragment as it starts to load. Its something like...
ImageView spinnerActionView = (ImageView) inflater.inflate(R.layout.refresh_action_view, null);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);
rotation.setRepeatCount(Animation.INFINITE);
spinnerActionView.startAnimation(rotation);
menuItemRefresh = menu.findItem(R.id.menu_refresh);
menuItemRefresh.setActionView(spinnerActionView);
Where menu
is assigned to an instance variable of the container when onCreateOptionsMenu
is called.
Update:
I've noticed another weird bug in this area (I'm happy to add this as a separate question, but I'm updating it here in the hope that it will shed some light on my original problem - I believe both are caused by how I have configured my action-bar from my Fragments).
When I first load a fragment I have 1 static refresh icon displayed. If I rotate the screen... another refresh icon appears... when I rotate the screen back, a 3rd refresh icon appears!
Stranger still, clicking the back-button removes each additional icon in turn, before finally (on the 4th click) returning to the previous screen.