I have nested fragment, fragment inside fragment. Using NavigationBottomView. Everything goes right when I backpress from parent to child fragment.
But now, if I am inside child fragment and suddenly my phone screen goes off, and if I Open the screen again and backpress from where I left, it's not going I think it is forgot or clear my backstack. Don't know why. I have made a demo on github link below
MessageListningFragment messageListningFragment = new MessageListningFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack( null );
transaction.replace( R.id.dynamic_container, messageListningFragment ).commitAllowingStateLoss();
github demo link is here
You should either use a ViewPager/ViewPager2 with NavHost fragments or implement BottomNavigationView with extension functions Google provided, which basically tags fragments and add them with NavHostFragment.create().
NavHostFragment takes care of childFragmentManager and back stack management. You can check out examples here if you wish.
Related
I CANT use ActionToolbar because this is a KIOSK app with a custom header fragment that needs to be displayed at all time. Hence my situation, I have a basic ImageButton on the said header fragment, and I want to implement the functionality of a basic ActionToolbar.
I add my fragment to the main container like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, UserListFragment.newInstance())
.addToBackStack("foo")
.commit();
The question is, how do I restore the state of my main_container before the current fragment was replaced with UserListFragment.
Like : fragment A is showing, replacing fragment A with B, pressing some button in my header fragment to pop the fragment B so again fragment A is showing.
You should consider switching to Android Jetpack Navigation Component it was created for your type of situation.
Check: https://stackoverflow.com/a/62252603/8714139
Cheers!
I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.
I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.
I asked this question before getChildFragmentManager() and viewpager without any responses, probably because the explanation was too long.
Simply put I have a viewpager inside a fragment. The pages inside that viewpager need to be replaced through a series of screens. Pressing back on those screens will not exit the application. How do I do this?
challenges:
viewpager xml does not use framelayout. when I use the replace method, what container
id do I use?
when replacing the fragments why would I use getChildFragmentManager each time if that is private to the current fragment?
#goonedroid: that's not the case I'm looking for here. The only similarity is that the fragment has a viewpager. My viewpager's pages need to be replaced when clicked:
I have a navdrawer. Clicking item 2 shows FragmentA. Clicking FragmentA replaces it with FragmentB. Clicking FragmentB replaces it with FragmentC. Clicking FragmentC takes you back to FragmentB. Fragments are added to the backstack for proper back navigation.
Clicking item 1 in the navdrawer shows FragmentZ with a viewpager. The viewpagers pages are just FragmentA and all the aforementioned behavior. But going from A to B here, then pressing back shows no pages in the viewpager.
I finally figured out a solution, not sure why this works because I thought an Activity's getSupportFragmentManager() returns the same fragment manager as its fragment's getFragmentManager().
Anyway, instead of getFragmentManager() in the fragments, use getActivity().getFragmentManager()
I've implemented navigation in my Android app as one Activity and many Fragments.
When I open new child Fragment, I call this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction() .addToBackStack(null).replace(R.id.fragment_container, f)
.setBreadCrumbTitle(f.getClass().getSimpleName());
tx.commit()
This works quite well, as old fragment isn't destroyed, so when I go back in the back stack, the old fragment retains the scroll position and everything entered on it.
The problem is, in one Fragment I actually need to pause something when I add another fragment above it. I've thought that onPause() will be called, but it doesn't - I suppose that all other fragments live beneath the ones on top of them.
On the side note, I also think that my app gradually slows down as the user navigates deeper. Shouldn't I be replacing the fragment in the transaction instead, so only one is on the top?