I have a FragmentActivity that contains a FrameLayout. I use the following code to add Fragments to the Fragment Activity.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment, fargmentTag);
ft.addToBackStack(null);
ft.commit();
Now R.id.fragment_content is FrameLayout. This adds a Fragment onto the view. But the underlying view still remains visible. Meaning the one lying view is seen through the new fragment ? What am i doing wrong.
Kind Regards
for the next fragment use ft.replace(); so the previous one will be repalced.
Either ft.remove() the old fragment, or use ft.replace() instead of ft.add().
ft.replace() should work fine, otherwise you have to update your question for a better understanding.
Using ft.replace will replace any previously added fragment with this fragment but if the activity has some view added to its layout rather than a fragment ,then the fragment added through add or replace will show its content over it rather than replacing it as it is not a fragment.
either add another layout within your main layout and assign it some id and then add the fragment to that container.
Hope it helps.
Related
As the title suggest, is it a good idea to perform FragmentTransaction inside a fragment?
The way I view this is that, FragmentTransaction requires a containerView id and the Fragment to inflate when calling FragmentTransaction.replace(). The containerView where to get the ID usually resides in the activity layout. So it seems like a good idea to do FragmentTransaction inside the activity rather than in the Fragment.
But I got confused when using SharedElementTransition for fragments. Now it requires a list of Views which will be the sharedElements. These views reside in the Fragment. Now it seems logical to do the FragmentTransaction inside the Fragment itself.
Can anyone guide me on how to compensate for both scenarios?
There's nothing wrong on doing FragmentTransaction inside a fragment as long as it is not redundant, like for example:
"Container" Fragment
"Child Fragment" that holds view pager
View pager has fragments
Here, you can remove the child fragment and hold the view pager on container fragment instead.
For FragmentTransaction inside a fragment, you can actually swap the fragment itself by calling getActivity().getSupportFragmentManager(). Here, transaction is happening on Activity not on the fragment. But if you intended to use FragmentTransaction to swap the child of the parent fragment, you can use getChildFragmentManager() on the fragment.
Also, child fragments is actually supported by android.
I have an activity with a fragment container layout and in that layout i load a fragment A which has a listview (listview loads data from web). Now on clicking listview item i want to show detail view. For that i am loading another fragment B which is detail fragment. Now to do that first i used
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
But using replace remove the previous fragment A which contain listview and on pressing back it again reloads the onCreateView method of fragment A and its listview data. I want to prevent it from loading fragment A again (or sending call to webserver) on backpress. I want to maintain fragment A state and its listviews scroll position. To do that i used second option which is
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
If i use add() then Fragment B loads on top of Fragment A and Fragment A still visible. It shows both fragments overlapping each other and transparent. I also came across anothe solution which is to use show() and hide() methods. But i dont think that is an efficient way to achieve that. Can anyone suggest an effective solution?
I need to replace one list fragment with another one (also list). But they overlaps.
Fragments were added dynamically:
fragmentManager.beginTransaction().replace(R.id.container,
CatalogFragment.newInstance(position + 1)).addToBackStack(null).commit();
and I'm trying to replace this CatalogFragment with LessonFragment in OnFragmentInteractionListener when the item of CatalogFragment's list item clicked like this:
// fragmentManager = getSupportFragmentManager();
// fragmentManager.popBackStack();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_catalog, LessonsFragment.newInstance());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I've been trying to pop previous item in backStack. Also tried to give the name of backStack of CatalogFragment and then remove it manually. Doesn't work.
Both of fragments are getting displayed at the same time.
I have tried first 10 solutions of google and tried to use standard developer.android's solution of replacement. Fail.
Please help if you know how. Thanks in advance.
P.S. Tell if some more code needed.
From your code, you added CatalogFragment in a layout: R.id.container, but you replace your LessonsFragment in another layout: R.id.fragment_catalog. I think that it's the reason why 2 fragments display at the same time. You should only use 1 Layout view to display fragments with you want only 1 visible.
I have developed an android application by extending to FragmentActivity. Everything works exactly as it done. I have put NAVIGATION_MODES as TABS. I have a LISTVIEW inside TAB A of FRAGMENT A. When the user clicks on LISTVIEW ITEM, new Fragment/ACTIVITY should launch showing the same ACTION BAR and same TABS. How can I achieve this.
Please suggest any methods.
Thanks
Get the container of the current Fragment A with the ListView and replace the Fragment inside
so in the onclick method of the listview adapter:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(FragmentA.this.getParent().getID(), mNewFragment);
ft.addToBackStack(null);
ft.commit();
since FragmentA.this.getParent().getID() is very clunky, search for the ID of the Container where FragmentA is located in, and use it instead. R.id.FragmentAContainerID
I have looked around the internet and I have found only examples of Android fragmens, where they are placed on place (like two of them, one as a list, second is diplaying details of a selected item from list). My goal is to have classic android activity and I want to be able to add to it dynamically pre-prepared fragments with particular GUI. I want to add there dome group of EditText elements (editTextFragment), or several radiobuttons (radionbuttonFragment). Is there any example of how to do that? Making stable fragments is not usable for me.
Thanks
This answer is for those who all searching the answer for same question.
The answer is achieved by FragmentManager and FragmentTransaction with the following syntax.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
then you need to add your fragment to this fragmentTransaction with the following syntax.
fragmentTransaction.replace(R.id.detailFragment, layout1);
And finally you MUST commit your transaction. Otherwise changes will not persist.
fragmentTransaction.commit();
Fragment and views are very similar so think similar... Also look at google api demoes for fragments. If I remember correctly they use a framelayout and adds fragments to it.
You can add and remove fragments to a FrameLayout in an activity's UI programmatically but your activity must extend FragmentActivty. Once you have created fragment classes and corresponding layouts, instantiated fragments can be added and removed via the FragmentManger and FragmentTransaction. See the section "Performing Fragment Transactions" in the Fragment documentation.