- How to clear navigation stack after navigating to another fragment in Android
- clear navigation stack after navigating to another fragment
- Here is complete Example:
- MichaЕ‚ ЕЃuszczuk
- Personal thoughts about code, Android and more
- Android Fragments restoration mechanism
- Beginning
- The Question
- 1. Saving the state
- 2. Destroying fragments and activities.
- 3. Creating activity and recreating fragments
- Conclusion
- Android Fragments Common Queries & Common Mistakes
- getSupportFragmentManager and getChildFragmentManager
- Callback from DialogFragment, ChildFragment, BottomSheetFragment to parent fragment
- Fragments when using ViewPager and adapters FragmentStateAdapter and FragmentPagerAdapter which one one to use when
- amodkanthe/ViewPagerTest
- Contribute to amodkanthe/ViewPagerTest development by creating an account on GitHub.
- FragmentTransaction add vs replace
- Fragment receivers, broadcasts and memory leaks
- Fragment BottomBarNavigation and drawer how to handle these
How to clear navigation stack after navigating to another fragment in Android
clear navigation stack after navigating to another fragment
In this tutorial we will discuss about Android Navigation library which is part of Jetpack. This library help us in managing navigation in our android app by creating single Activity, Navigation graph etc. But as you start using Android Navigation you may face few problems. So we will discuss about one problem in this post.
So if we want to navigate to another fragment and we want to clear stack (remove all fragments which were opened previously) then we can tweak this by simply navigating to root of Navigation with the help of Navigation graph and then navigate to our destination fragment. Fortunately we don’t need to this all stuff manually. We can do it easily with following method.
In your Navigation Graph add new Action from Fragment1 to Fragment2 and inside that action add following properties popUpTp=“Navigation _ Graph _ Id” and popUpToInclusive=“true” and rebuild your project. After rebuilding project navigate to that newly created direction and that’s it.
Here is complete Example:
Code of res/navigation/main _ graph.xml
After pasting above code in Navigation Graph, rebuild your project. Now do the following in current fragment
Now you will redirect from SecondFragment to ThirdFragment and FirstFragment and SecondFragment will be cleared from stack.
Источник
MichaЕ‚ ЕЃuszczuk
Personal thoughts about code, Android and more
Android Fragments restoration mechanism
In my last post, where I described a problem of incorrect usage of Fragments instantiation inside FragmentPageAdapter & ViewPager , I wrote:
After orientation change all fragments currently added to FragmentManager are automatically restored and instantiated so there is no need to create them once again.
Today, I want to focus more specifically on an issue how this automatic restoration works under the hood.
Beginning
The simplest way to use a fragment:
A custom fragment instance is created and added to FragmentManager with help of FragmentTransaction .
We could add that our Fragment will be added to container identified by android.R.id.content .
This is the easiest case. I want to point out that we are not using setRetainInstance(true) inside our custom fragment implementation, and our activity is not protected in manifest agains any type of configuration changes.
The Question
Now, what will happen with our fragment if suddenly our device configuration will change, i.e. orientation?
Android Source Code (especially FragmentActivity and FragmentManager / FragmentManagerImpl ) is a place where we should look for the answer.
1. Saving the state
Before Activity will be destroyed its onSaveInstanceState method will be called. Take a look what this method is doing internally.
It takes mFragments (reference to FragmentManager held in this Activity) and calls FragmentManager.saveAllState() method which will return a parcelable object ready to be saved inside the bundle which, you can already guess… later will be used to restore Fragments.
In reality result of saveAllState method call is an object of type FragmentManagerState which consists of information about all active fragments and back stack entries
Last quick look at FragmentState .
It consists of all data which describes a specific fragment object instance, like container id, tag, arguments but also savedFragmentState.
It looks sufficient to create fragments from scratch, and set them like they were before.
2. Destroying fragments and activities.
After state of fragments is saved via FragmentManager activity object is destroyed (removed from memory) with all its fragments (those which are not retained with setRetainInstance(true) ).
3. Creating activity and recreating fragments
Final point is a recreation of the activity and recreation of the fragments. It starts within the first Activity lifecycle callback method onCreate(Bundle savedInstanceState)
The previously saved fragment manager state bundle now is used inside FragmentManager.restoreAllState method. This metod declaration is quite long but I want to focus on the most important part.
Array with all FragmentState objects is iterated. And every FragmentState object is used to recreate (create new instance with state like before) specific Fragments.
A new instance of a fragment is created by platform with usage of reflection and default constructor – that’s why you must remember to always ensure the existence of public non-argument fragment constructor and initialize your fragment through arguments bundle (not with usage of fragment object setters from strange places).
This is short story how restoration of Fragments works.
Conclusion
Points to remember:
- Already created fragments are restored automatically after orientation change
- Magic behind this is just code written in Activity together with FragmentManager logic/implementation
- Avoid setter methods and parametrized constructors to modify fragment state, because platform uses only default (0 parameter) constructor, arguments bundle and saved state bundle to restore it later
- Fragments are not so bad
Источник
Android Fragments Common Queries & Common Mistakes
Fragment class in Android is used to build dynamic User Interfaces. Fragment should be used within the Activity. A greatest advantage of fragments is that it simplifies the task of creating UI for multiple screen sizes. A activity can contain any number of fragments.
Now this meaning of fragment sounds good and easy, right? But there is lot more involved, this article covers main needs and common mistakes while using Fragments.
I am assuming you are having basic knowledge of Fragment and Fragment lifecycle callbacks also I am assuming you know how implement communication between two fragments this article goes beyond that
So here are a few obstacles related to fragments some of you must have faced already, some of you might face later.
- FragmentManager: getSupportFragmentManager and getChildFragmentManager. Which one to use when and avoid memory leaks while using them.
- Callback from DialogFragment, ChildFragment, BottomSheetFragment to parent fragment.
- Fragments when using ViewPager and when to use FragmentStateAdapter vs FragmentPagerAdapter.
- When to use FragmentTransaction add vs replace ?
- Fragment receivers, broadcasts and memory leaks.
- Fragment BottomBarNavigation and drawer. How to handle these?
- commit() and commitAllowingStateLoss()
- Fragment option menus.
- Fragment getActivity(), getView() and NullPointers Exceptions.
- onActivityResult with nested fragments.
- Fragment and Bundle.
- Back Navigation.
Whoa . see its a big list, reply in comment if anyone wish to add something more to the list.
getSupportFragmentManager and getChildFragmentManager
FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments.
- getSupportFragmentManager is associated with Activity consider it as a FragmentManager for your Activity .
So whenever you are using ViewPager, BottomSheetFragment and DialogFragment in an Activity you will use getSupportFragmentManager
- getChildFragmentManager is associated with fragment.
Whenever you are ViewPager inside Fragment you will use getChildFragmentManager
Here is official link for this for better understanding.
Now coming to common mistakes people do when they are using ViewPager inside a Fragment they pass getSupportFragmentManager which is fragment manager for Activity and it causes issues as such memory leaks, ViewPager not updated properly sometimes etc.
Most important issue caused by using getSupportFragmentManager in Fragment is memory leak, let me tell you how? Your Fragment has stack of fragments which is used by ViewPager or any other thing and all these fragments stack is in activity since you used getSupportFragmentManager , now if close your Parent fragment it will be closed but it will not be destroyed because all child Fragments are in activity and they are still in memory which does not allow to destroy Parent Fragment hence causing leak. It will not just leak parent fragment but also leak all child fragments since none of them can be cleared from heap memory. So never try to use getSupportFragmentManager in a Fragment
Callback from DialogFragment, ChildFragment, BottomSheetFragment to parent fragment
This is very common issue people face when they use BottomSheetFragment or DialogFragment or ChildFragment.
Add a child fragment
Another example bottomSheetFragment
Now suppose you want callback from these child fragments to parent fragments . Most of people create connection between two fragments using activity, few people pass interface listeners as a parameter to fragment which really a bad practice and one should avoid this. Best way calling getParentFragment() from your child fragment to create a call back this is very simple way consider example below.
then setting callback to parent fragment add following code in child fragment.
thats it you can give a callback to your parent fragment now easily.
Using same way you can create a callback from child fragment inside ViewPager to parent fragment who is holding ViewPager.
Fragments when using ViewPager and adapters FragmentStateAdapter and FragmentPagerAdapter which one one to use when
FragmentPagerAdapter stores the whole fragment in memory, and could increase a memory overhead if a large amount of fragments are used in ViewPager . FragmentStatePagerAdapter only stores the savedInstanceState of fragments, and destroys all the fragments when they lose focus.
So when your is going to have many Fragments use FragmentStateAdapter if ViewPager is going to have less than three fragments use FragmentPagerAdapter.
Commonly faced issues
Update ViewPager not working:
People always come across the issue remember ViewPager fragments are managed by FragmentManager either from Fragment or Activity and this FragmentManager holds instance of all ViewPager Fragments.
So when people say ViewPager is not refreshed it’s nothing but old instances of fragments are still being hold by FragmentManager. So you need to find out why FragmentManger is holding instance of Fragments is there any leak or not ideally to refresh ViewPager following code works if it is not you are doing something wrong
Access current Fragment from ViewPager:
This is also very common issue we come across. For this people either create array list of fragments inside adapter or try to access fragment using some tags according to me both methods are not reliable. FragmentStateAdapter and FragmentPagerAdapter both provides method setPrimaryItem this can be used to set current fragment as below.
I am leaving a Github link below to this simple ViewPager project so that everyone can understand better.
amodkanthe/ViewPagerTest
Contribute to amodkanthe/ViewPagerTest development by creating an account on GitHub.
FragmentTransaction add vs replace
In our Activity we have a container and inside this container we display our Fragments
add will simply add fragment to container suppose you add FragmentA and FragmentB to container. Container will have FragmentA and FragmentB both and suppose if container is FrameLayout fragments are added one above the other. replace will simply replace a fragment on top of container so if I call create FragmentC now and call replace FragmentB which was on top will removed from container unless you are not calling addToBackStack and now FragmentC will be on top.
So which one to use when. replace removes the existing fragment and adds a new fragment . This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in ‘paused’ state hence when a back button is pressed onCreateView is not called for the existing fragment(the fragment which was there before new fragment was added). In terms of fragment’s life cycle events onPause, onResume, onCreateView and other life cycle events will be invoked in case of replace but they wont be invoked in case of add .
Use replace fragment if don’t need to revisit current fragment and current fragment is not require anymore. Also if your app has memory constraints use replace instead of add.
Fragment receivers, broadcasts and memory leaks
Mistakes people commonly do when using receivers inside a fragment forgot to unregister receiver in onPause or OnDestroy. If you are registering fragment to listen to receiver inside onCreate or OnResume you will have to unregister it inside onPause or onDestroy otherwise it will cause memory leak
Also if have multiple fragments listening to same broadcast receiver make sure you register in onResume and unregister in onPause. Because if you use onCreate and onDestroy for register and unregister other fragments will not receive the broadcast as this fragment is not destroyed
Fragment BottomBarNavigation and drawer how to handle these
When we are using BottomBarNavigation and NavigationDrawer people face issues like fragments recreated, same fragment is added multiple times etc.
So in such case you can use fragment transaction show and hide instead of add or replace.
There is also one beautiful library which take care of navigations and avoid recreation of fragments called FragNav below is link to it
Источник