Reputation: 6546
I have 4+ Fragments (android.support.v4.app.Fragment
).
Fragment f1, f2, f3, f4;
int F1 = 1; int F2 = 2; int F3 = 3; int F4 = 4;
protected void onCreate(Bundle savedInstanceState)
{
//...
f1 = new Fragment();
f2 = new Fragment();
f3 = new Fragment();
f4 = new Fragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fl_root, f1)
.add(R.id.fl_root, f2)
.add(R.id.fl_root, f3)
.add(R.id.fl_root, f4)
.commit();
setFragment(F1);
}
public void setFragment(int fragment)
{
switch (fragment)
{
case F1:
if (!f1.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f1)
.hide(f2)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F2:
if (!f2.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f2)
.hide(f1)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F3:
if (!f3.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f3)
.hide(f1)
.hide(f2)
.hide(f4)
.commit();
}
break;
case F4:
if (!f1.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f4)
.hide(f1)
.hide(f2)
.hide(f3)
.commit();
}
break;
}
After setFragment
to first, second and so on (1->2->3->4) rtol
(translate from right to left) animation works, and it's fine.
But after going back, using setFragment
(4->3->2->1), there are still rtol
animation, instead of correct ltor
(translate from left to right).
What I'm doing wrong, and what is the solution?
Upvotes: 2
Views: 7346
Reputation: 6546
Actually there are 20+ Fragments in Single Activity in my Project.
I solved this animation problem + OutOfMemory
(because of 20+ fragments added and hided).
A little bit code:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (!back)
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
else
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right);
switch (fragment)
{
case F1:
ft.replace(R.id.fl_root, new F1(), String.valueOf(F1));
break;
//...
}
Upvotes: 2
Reputation: 306
The setCustomAnimations method takes one anim for entering and one for exiting a fragment so even if you go back, you are entering one of these fragments.
So you have to take a look at the fragment you are coming from and switch the animations.
Edit:
Maybe like this untested
int lastFragment;
public void setFragment(int fragment)
{
FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
if(fragment > lastFragment) {
ft.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0);
} else {
ft.setCustomAnimations(R.anim.ltor, R.anim.rtol, 0, 0);
}
switch (fragment)
{
case F1:
if (!f1.isVisible()) {
ft
.show(f1)
.hide(f2)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F2:
if (!f2.isVisible()) {
ft
.show(f2)
.hide(f1)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F3:
if (!f3.isVisible()) {
ft
.show(f3)
.hide(f1)
.hide(f2)
.hide(f4)
.commit();
}
break;
case F4:
if (!f1.isVisible()) {
ft
.show(f4)
.hide(f1)
.hide(f2)
.hide(f3)
.commit();
}
break;
lastFragment = fragment;
}
Upvotes: 0