Replace Main Fragment with Child fragment - android

I have 3 fragments Fragment A is the main fragment and Fragment B is a child fragment. When clicking on fragment B , I want fragment C to replace the whole fragment A and navigate to it again when button back is pressed from C.
The problem that i can't solve is that the framelayout container is in Fragment A xml so what fragment container i should give to fragment C :
Fragment fragment = new PetDetailFragment();
String fragmentTag = fragment.getClass().getName();
getFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment, fragmentTag).commit();

when the user clicks on fragment B just execute
getFragmentManager().beginTransaction().add(R.id.frameLayout, new FragmentC(), fragmentCTag).commit();
and then when the user presses back fragment A will be visible automatically. To display fragment A manually execute
getfragmentmanager().popbackstack()

Related

Navigation Component: Navigate from Fragment in Viewpager to another Fragment

I have a Fragment A which navigates to Fragment B
Fragment B hosts a ViewPager2.
ViewPager2 has two fragments inflated in it - Fragment C and Fragment D.
On clicking an item in Fragment C I want to navigate to a new instance of Fragment D on top of Fragment B.
Currently, my code is something like this -
findNavController().navigate(
R.id.action_FragmentC_to_FragmentD,
bundleOf("id" to id, "type" to "list")
)
However, when I try to navigate I get the following exception -
java.lang.IllegalArgumentException: Navigation action/destination com.test.at:id/action_FragmentC_to_FragmentD cannot be found from the current destination Destination(com.krtkush.audiotime:id/FragmentB) label=FragmentB
From what I can understand is that the navigation component is still on Fragment B but I'm trying to navigate to Fragment D from Fragment C and hence it is throwing an exception.
I (think) can fix this by adding a listener which informs Fragment B when the item is tapped in Fragment C and then navigate to Fragment D from Fragment B. However, this will make my Fragment C tightly coupled to Fragment B which I don't want to. Anyway to fix this is another way?
If you do not navigate() to Fragment C and Fragment D, you will remain on the last destination you navigated to: Fragment B, that is the expected behavior. The NavController knows nothing about child fragments such as fragments within a ViewPager of Fragment B and therefore you were never on Fragment C as a destination.
If you never navigate() to Fragment C and it is only viewable as part of your ViewPager, then Fragment C should not be in your graph. Any actions from fragments within Fragment B's ViewPager should be actions on Fragment B directly (the destination you're actually on).
Note that you can reuse the same actions names on fragments such as Fragment D if you are also using them as standalone destinations in your graph (thus ensuring that the action is available in both cases).

Open inner fragment on clicking a button

In My application I have 5 tabs.
In 1st tab when user clicks a button I need to go to 2nd tab which contain 3 fragments.
1) List Fragment
2) Details Fragment and
3) Result Fragment
When user click on a button I want to go directly to the Details Fragment, and when clicks back it should go to the list.
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
In your code instead of calling getActivity().getSupportFragmentManager() call getActivity().getchildfragmentmanager()
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getChildFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();

Fragments navigation

I need to create navigation of fragments like in Gmail app. It's like: we have one main fragment A, we can open another fragment (B,C,D...) from navigation drawer, and when we open new fragment, it`s open on top of main fragment, and when press back button, in all cases we come back to main fragment A, don't depend of count new opened fragments. It's seems, first main fragment A we use add method(int FragmentTransaction) without adding to fragment backStack. Then, next fragment B we use method add too, with adding to back stack. And when I need to open another one (Fragment C), I need to replace second fragment B. But, when I use method replace(), replaced all container, and main fragment A not showing when back button pressed from fragment C or B and app close. So, the question is: how to replace only fragment B or C, without losing fragment A?
A valid solution would be to have two container framelayouts in your activity. The first one (which will below the other one) contains your fragment A. Everything you open will be added/replaced in the second container.
Another solution is to include the fragment A statically in your layout and have your container framelayout on top of it where you add your fragments B, C, D etc.
open fragment Like this
HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this); //Your fragment
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, highlightFragment) // LL_fragment is container
.addToBackStack(null)
.commit();
and in Activity OnBackPress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Android: Get Parent View's Framelayout from Fragment inside TabHost

I have a FragmentActivity in which I am displaying two Fragments, Fragment A and Fragment B. In Fragment A I have another Fragment with Tabs. I have done this using classic TabHost. In this TabHost I have 2 more Fragments which has a ListView. Now what I want is on clicking on listitem I want to replace the content in Fragment B of parent FragmentActivity. Please help me to achieve this. I have tried the following till now.
1. View mContainer = (View)getActivity().findViewById(R.id.rightpane);
2. ViewGroup mContainer = (ViewGroup)getView().getParent();
3. Activity rrlist = ((RRPatientList)getParentFragment()).getActivity().getParent();
View mContainer = (View) rrlist.findViewById(R.id.rightpane);
ResultDetailView rdl = new ResultDetailView();
rdl.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(mContainer.getId(), rdl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
Nothing is working and I am getting either NullPointerException or No View Found for the ID ****** errors.
Please refer the screen shot for more info.
create method(like: updateFragment) in your activity to update fragment B. from fragment A's child fragment call that method(updateFragment) then that method(updateFragment) update fragment B.
here is answer for update fragment from activity - it 'll help you to update fragment B from your parent activity
here is answer for call activity method from fragment - it 'll help you to call parent activity method from fragment A's child fragment

When fragment calls fragment, who is the parent of second fragment?

So I have started fragment A from my main activity and that fragment contains a button. When that button is clicked this code runs :
Fragment newFragment = new HomeFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.formFragment_Container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So, as you can tell, I fire up fragment B from fragment A and what I don't get is what activity hosts that fragment B, since there is no indication of main activity in this code above, nor there is indication of fragment B in main activity. Could you clarify this for me. Thanks !
When you call to
getFragmentManager()
It use instance of FragmentManager that the Activity use to manage it Fragments. So the Activity will be the same as Activity A.
It comes under the concept of nested fragments in Android. For both Fragment A and B, its associated activity is main activity(from which you have added Fragment A).
You can access Main activity and Fragment A from Fragment B.
For example. getActivity() will return Main Activity, getParentFragment() will return Fragment A.

Categories

Resources