Switch between fragments from inside one of the fragmens - java

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.

Related

Previous fragment is restarting when we go back to previous fragment using backstack in android

lets say we have two fragment and we are in second fragment then if someone press back button then we successfully go back to the previous fragment (i.e. first fragment) using backstack but the problem is that the previous fragment is restarting everything. I want to display content of previous fragment without any restarting or initializing when we press back button.Thanks in advance.
Welcome to Android Fragment Life !!
As suggested by #Uuu Uuu, you need to use add() method while adding fragment. You are getting the fragment overlapped because there is a new fragment added each time.
You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-
if (fragmentManager.findFragmentByTag("First Fragment") == null)
fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();
If you are new to android development, please learn about the fragment/activity life cycle, there is a wonderful article By Jose Alcérreca
I hope this will help, happy coding.

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

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

Android - Replacing the N-th fragment?

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.

Android: Method in Fragment Lifecycle

I'm looking for a method in the Fragment Lifecycle, but I'm not sure which one.
Here's my situation: I've got a Fragment inside a ViewPager. The Fragment displays a List with some information. I fill the list in the Fragment's onCreateView(). When the user opens a different Activity (settings in this case) and changes some settings, the information that the List in the Fragment has to show, changes. When the user returns to the Fragment using the Back-button, the onCreateView() isn't re-called, so the information in the List isn't updated.
My question is: The onCreateView()-method isn't called when the user returns to the fragment form a different Activity, but which method is called here? I need to know this because then I can fill the List in that method.
Thanks in advance!
Important and non-obvious point it that Fragment's onCreateView() being called not only in case you selected Tab with this Fragment. So don't rely on onCreateView() of Fragment when using ViewPager(). When You select the Tab, Android creates sible views (caches them) or makes something similar.
You should call your update method when user selects proper Tab in ViewPager (don't remember exactly, but hope it helps).
onResume() is the simple answer, called when user comes back. for more details refer lifecycle here FragmentLifecycle

Removing a fragment in another fragment activity

I have a fragment that implements an onClickListener, and then the onClickListener tries to remove a fragment.
I get a red line under badFragment in transaction.remove(badFragment);. My best guess is because it can't tell what that fragment is/where it is.
How would I go about finding that fragment, so that it can be removed?
I've tried findFragmentId(badFragment); but it doesn't work.
Bonus points if you can let me know where you found this method. As I'm not great at looking up things in the Android Docs.
You can find fragment
getFragmentManager().findFragmentById(R.id.fragment_id);
or
getFragmentManager().findFragmentByTag("tag");
you can remove fragment itself
getActivity().getFragmentManager().beginTransaction().remove(this).commit();

Categories