I am new to Android Development. I am using single FragmentActivity and it contains FrameLayout. I use many fragment classes.Some times when I replace one fragment to another their layouts are merge with each other. How can I solve this problem?
Are you doing the following ??
ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack(null);
ft.commit();
Where ft is your FragmentTransaction, and fragmentManager is your
FragmentManager fragmentManager = getSupportFragmentManager();
use replace to add new fragment
ft.replace(id,newfragment);
and remove the old fragment
ft.remove(old fragment);
Related
I have 3 fragments added to stage in one and the same container. Two fragments are added with addToBackStack method:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
And final one is added without back stack:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
All three co-exist fine because as I understand if a fragment is added using a backstack, then it's not destroyed when another fragment is added.
The last one is added without a backstack, meaning that once I call replace it should be destroyed.
However, this does not happen and when I call
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, myNewFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I can still see that there are 4 fragments total in my R.id.container, which is not that I expect.
How can I make my 3rd fragment disappear when I call replace method when adding 4th fragment?
By calling addToBackStack(), the replace transaction is saved to the
back stack so the user can reverse the transaction and bring back the
previous fragment by pressing the Back button.
So in your case you are replacing the third fragment with the fourth fragment . It means for reversing the transaction they need fragment3 also . So they will not destroy it.
I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
I have a situation in which I need to show/hide a fragment based on a dynamic variable. My question is should I create the fragment once, and then show/hide based on the variable. Or should I destroy/create it each time?
Create the fragment
Fragment alertFragment = new AlertFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, alertFragment).commit();
After this should I call show/hide each time?
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(alertFragment) // or hide
.commit();
Seems like a lot of work to show/hide something each time. Is this the right way to do it?
I'd say that depends on weather it matters for your application if your Fragment is destroyed or not.
If it doesn't matter at all, simply replace the Fragment everytime by a new one. This is the simplest solution and does not require any logic.
Fragment f = new Fragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, f).commit();
I have a actionbar tab setup with fragments for each of the pages. (extended default example from wizard).
I'm attempting to create a new fragment and make it the current fragment, with the other hidden
ConfigDetailSectionFragment configDetailSectionFragment = new ConfigDetailSectionFragment();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_main_config, configDetailSectionFragment);
transaction.addToBackStack(null);
transaction.commit();
the code shows the new fragment but seems it is transparent and I can see the fragment below.
i'm probably forgetting to do something... what am i forgetting to do or am missing?
Adding an android:background="#FFFFFF" fixed the issue, per beworker. Thanks!
In my android application i am using fragments and i wnat to get the displayed fragment by tag for that i am using below code to get the fragment by tag and i am using below code to replacing the current fragment with another fragment i am now understanding what is the wrong in my code so any one guide my to correct way.
DigitalDisplayFragment myDigitalDisplayFragment = (DigitalDisplayFragment)getSupportFragmentManager().findFragmentByTag("DisplayFragment");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, displayFragment, "DisplayFragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();