I have a user interface divided into two blocks, a menu panel which is the same all the time, and a content which changes depending on the menu entry which is selected.
To change the content fragment dynamically i have to replace the current fragment by the new one, as explained in this SO question : Replacing a fragment with another fragment inside activity group
The problem is that the "current" fragment is not only one fragment, but two : the panel AND the content. So if I attempt to replace i assume that both fragments will be replaced by the new content fragment, which is not what i want.
Is there a way to replace one particular fragment with removing other present fragments, if any ? Thank you.
You can easily replace single fragment with FragmentTransaction#replace method.
Suppose you want to replace a fragment in a Framelayout, you can do it as shown below:
getFragmentManager().beginTransaction()
.replace(R.id.container, newFragment, null).commit();
Here R.id.container is id of the FrameLayout and newFragment is the Fragment you want to put in layout.
I guess you have to have two fragments in the content block. For the first fragment you would be setting a weight of 1 so that it occupies full content block unless you want your second fragment in the block to be visible.you would be setting visibility of that second fragment in content block to "gone" if you want only one fragment to be visible or else give it some weight or layout width and layout height for both the fragments to be visible.
Related
I am building parts of my layout programmatically.
My process looks like this:
I inflate a layout: View view = inflater.inflate(R.layout.view_layout, viewGroup, false);
I add this view to the ViewGroup: viewGroup.addView(view);
Before adding views in this way, I first add a fragment to the root view of viewGroup:
getChildFragmentManager().beginTransaction().add(R.id.viewgroup_root, fragment, "fragment_tag").commit();
My ViewGroup is a LinearLayout and I am adding the Fragment before I add the other views. However, the Fragment is appearing last - after all the views I add using ViewGroup.addView().
How can I get the added Fragment to be displayed first in the LinearLayout, and why is the LinearLayout displaying it last if it was added first?
commit() is asynchronous - it does not run immediately. Therefore you definitely are running your addView methods before your fragment is actually added.
As Fragments automatically are re-added to their respective layout based on ID and you do not control the ordering of when the fragment is added to the layout in those cases, you can't rely on any initial ordering.
Instead, you should always add a Fragment to its own container - if using Fragment 1.2.0 or higher (the latest right now is 1.2.5), you should add a FragmentContainerView to your LinearLayout. If you're using an earlier version of Fragments, you'd want to add a FrameLayout to your LinearLayout. In either case, you'd need to make sure you use setId() with that layout and use the same ID when you use add.
You will have to leave placeholder under your LinearLayout and use placeholder's ID to do beginTransaction/add stuff. Any viewgroup would do. You are not just adding another view to your viewgroup, you are adding a fragment. It has its own lifecycle, bound to entities hosting it (activity, fragment).
I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html
I have one Activity with a viewpager in it and also a frameLayout which is used as a container for fragments that should be drawn above this viewpager.
When I click a button of my Activity a fragment is shown in that framelayout container. On a press in the navigation bar I can close this fragment. All these things work fine when called from the activity itself.
I have now the situation that on a specific action from one of the fragments in the viewpager I invoke a callback which the main activity implements and then shows a fragment in the frame layout.
But here now comes the problem. When I want to navigate back or rather want to close the fragment like I described before, nothing happens.
So in conclusion:
"Fragment show to container" invoked from Activity directly -> I can navigate back from the fragment by getActivity().onBackpressed()
"Fragment show to container" invoke from fragment in viewpager and then delegated to activity -> The navigation back or getActivity().onBackpressed() has no effect.
So my question is: How can I close the fragment in the container even when I call it from one the fragments inside the viewpager of the activity?
Probably you are adding a fragment to the backstack, so onBackPressed you should pop last fragment from the backstack.
Something like that:
FragmentManager fm = activity.getFragmentManager();
fm.popBackStackImmediate();
Meanwhile I found the problem..
Due to the fact that I am running some kind of update loop, I added the fragments multiple times to the backstack but didn't realize it because the fragment itself didn't change, the new instances just got added to the backstack. So when I called the onBackpressed() method the instances got removed from the backstack but because there were like 200 of them nothing happened.
Two days ago i asked the following question:
Change the fragment in a framelayout from within another fragment of said framelayout
A fellow user Krish, really helped me out in finding out what was wrong with the way i thought, but i am stil not sure how to actually get what i want done.
I want to be able to switch between three fragments in one FrameLayout.
- the first of the three is loaded at the start of the parent fragment and when back is pressed at the second fragment
- the second must be replacing the first at the click of an item in the listview of the first fragment, and when the back button is pressed from the third fragment
-the third must be loaded when a button is pressed in the second layout
I've tried achieving this by calling the following line whenever the fragment must be changed. A1_frame is the FrameLayout of the parent Fragment/Layout and A1_B0_C2 is the fragment i am replacing
getChildFragmentManager().beginTransaction().replace(R.id.A1_Frame, new A1_B0_C2()).addToBackStack(null).commit();
From what i understand the problem with my solution is that it isn't possible to replace a fragment in the FrameLayout of a parent Fragment/Layout, but if it would work, it would solve my solution. thats why i chose to put it in here.
I hope someone is able to tell me what would work!
getChildFragmentManager() returns the Fragment manager of the Fragment it's being called from, in this case whatever Fragment is in A1_Frame
The method you're looking for is getFragmentManager(), which returns the Fragment Manager of the Activity/Fragment that the Fragment is a part of. I.e. MainActivity, or whatever is creating your first fragment.
i am using a tabHost for 4 fragments in this fragments i am calling custom adapters for each fragment when i move from one fragment to another fragment my list view is reloading freshly i dont want to refresh my listview from moving one fragment to another
Help me guys
Make your data object the member of your activity that is initializing the fragments.
Check this out. I would recommend using a ViewPager with a tabbed indicator, or a set of custom buttons linked to the pager. I've implemented this solution in several apps where each fragment contains heavy content, and the performance was great. Create all your fragments, and setup the pager on each change, this way the content of your "tabs" will stay the same on user switching.