I've been trying to make the Transition Animations work with Fragments, what I find weird (or maybe I'm not understanding something right) is that when using replace it works as expected, but when using add it doesn't...
WORKS:
EndFragment fragment = new EndFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.addToBackStack(null)
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.replace(R.id.container, fragment)
.commit();
DOESN'T WORK:
EndFragment fragment = new EndFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.addToBackStack(null)
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.add(R.id.container, fragment)
.commit();
Any ideas?
Related
I have 4 java class that i use as fragments: `F1,F2,F3,F4.
When i want to swich from one to other, i use this code:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.commit();
FM.executePendingTransactions();
If i want to make possible to return from one of them to the previous, i add addToBackStack(TAG) as following:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.addToBackStack("F1");
FT.commit();
FM.executePendingTransactions();
I have only one activity that chages the current fragment displayed.
I would get from it, the current fragment displayed.
I tried to write this, but now is always null.
Fragment now=getSupportFragmentManager().findFragmentByTag("F1");
if(now!=null && now.isVisible()) {
//some code for the current fragment
}
Try This it may be help to you
Fragment currentFragment = getFragmentManager().findFragmentById(R.id.where);
if (currentFragment instanceof F1) {
//do your stuff here
}
I'm adding a Fragment to my Activity like this:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(fragment.getClass().getName())
.commit();
But when I want to find the Fragment using a FragmentManager it's returning null:
Fragment oldFragment = (Fragment) getSupportFragmentManager().findFragmentByTag(fragment.getClass().getName());
You try to find it by tag, but you haven't given it any tag
if you want to give it a tag, do it like this
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_container, fragment, "tagABC")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(fragment.getClass().getName())
.commit();
and then you can get it with
Fragment oldFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("tagABC");
BTW, you should correct your question title, the problem has nothing related to backstack.
I use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one
all I need is it has to replace the existing and place the new one
As I have to reuse the current view when back button is pressed cannot use the replace
Fragment fragment = null;
fragment = new DetailFragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container2, fragment)
.addToBackStack(null).commit();
If you don't use the support library (which is your actual case), use this code:
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction;
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container2,fragment.newIstance());
transaction.addToBackStack(null);
transaction.commit();
And you need to create newIstance in Fragment class:
public static fragment newIstance(){
fragment frag= new fragment();
return frag;
}
Following code am using while am adding new fragment and adding old fragment to backstack but still old fragment in backstack get click ,what is wrong with my code?
getFragmentManager()
.beginTransaction()
.add(R.id.content_frame, new XyzFragment())
.addToBackStack(null)
.commit();
use this, It is worked for me by using always "replace method" instead of "add method". I never used "add"
Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack("Frag");
fragment.setArguments(null);
ft.replace(R.id.content_frame, fragment);
ft.commit();
In my application there is a FrameLayout on which I am adding various fragments
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fargmentTag);
ft.commit();
Now it seems when I use the ft.replace(R.id.fragment_content, fragment, fargmentTag); and then in other places call
getSupportFragmentManager().findFragmentByTag(fargmentTag);
I always get null.
However if I use add instead of replace this problem is fixed but another problem appears that each fragment is added ontop of the other fragment and i can see the other fragments below.
I would prefer to use replace, but I need the saved fragment state. Also to be noted I am not using addToBackStack.
Try this and it would work.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fragmentTag);
ft.commitAllowingStateLoss();
You just basically answered your own question. Use addToBackStack().
FragmentManager: fm = getSupportFragmentManager(); fm.executePendingxxxx,try it.