Android Fragments Common Queries & Common Mistakes

amod kanthe
Apr 12 · 8 min read

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

example:

BottomDialogFragment bottomSheetDialog = BottomDialogFragment.getInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Custom Bottom Sheet");
  • getChildFragmentManager is associated with fragment.

Whenever you are ViewPager inside Fragment you will use getChildFragmentManager

example:

FragmentManager cfManager=getChildFragmentManager();
viewPagerAdapter = new ViewPagerAdapter(cfManager);

Here is official link for this for better understanding.

https://developer.android.com/reference/android/support/v4/app/FragmentManager.html

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.

Example:

Add a child fragment

FragmentTransaction ft = getChildFragmentManager().beginTransaction();
Fragment1Page2 fragment = new Fragment1Page2();
ft.replace(R.id.contentLayoutFragment1Page2, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Another example bottomSheetFragment

BottomSheetDialogFragment fragment = BottomSheetDialogFragment.newInstance();
fragment.show(getChildFragmentManager(), fragment.getTag());

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.

dialogFragment.show(ParentFragment.this.getChildFragmentManager(), "dialog_fragment");

then setting callback to parent fragment add following code in child fragment.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
callback = (Callback) getParentFragment();
} catch (ClassCastException e) {
throw new ClassCastException("Calling fragment must implement Callback interface");
}
}

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

List<String> strings = new ArrayList<>();
strings.add("1");
strings.add("2");
strings.add("3");
viewPager.setAdapter(new PagerFragAdapter(getSupportFragmentManager(), strings));
strings.add("4");
viewPager.getAdapter().notifyDataSetChanged();

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.

BlankFragment blankFragment;@Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
if (getBlankFragment() != object) {
blankFragment = (BlankFragment) object;
}
super.setPrimaryItem(container, position, object);
}
public BlankFragment getBlankFragment() {
return blankFragment;
}

I am leaving a Github link below to this simple ViewPager project so that everyone can understand better.


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

LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);

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


commit() and commitAllowingStateLoss()

If your activity is not in resume state and you try to commit a fragment app will crash to avoid this you need to check activity or fragment is in resume state or not isAdded() / isResumed()

Another way if you don’t care much about state of fragment you can call commitAllowingStateLoss this ensures fragments is added or replaced despite activity is finishing or not in resume state.


Fragment option menus

When using option menu inside Fragment remember to add following line many people forgot to add this and keep wondering where is options menu on toolbar

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

When using toolbar inside fragment can inflate menu using code

getToolbar().inflateMenu(R.menu.toolbar_menu_gmr);

Alternatively you can override on createOptionsMenu but I prefer above method as it does not rely on super class


Fragment getActivity(), getView() NullPointers Exceptions

If any background process posts result and fragment is not in stack or in resume state and you accessing view of fragment it will cause NullPointer exception so whenever you are accessing getView or getActivity after a back ground operation or delay make sure you cancel all background operation on destroy.

example

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(getActivity() != null && getView() != null && isAdded) {
PagerFragAdapter pagerFragAdapter = (PagerFragAdapter) viewPager.getAdapter();
pagerFragAdapter.getBlankFragment().setLabel();
}
}
}, 500);

Nested fragment onActivityResult

Yes, the onActivityResult() in nested fragment will not be invoked.

The calling sequence of onActivityResult (in Android support library) is

  1. Activity.dispatchActivityResult().
  2. FragmentActivity.onActivityResult().
  3. Fragment.onActivityResult().

you will have to use onActivityResult() in parent fragment or activity and to pass result to nested fragment as below

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}

Fragment and Bundle

When ever you are passing arguments to Fragment make sure you are using Bundle instead of constructor.

The Android documentation states:

Every fragment must have an empty constructor, so it can be instantiated when restoring its activity’s state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

That’s why it’s better to use a bundle and set the parameters of the Fragment this way, it’s easier for the system to restore its values when the fragment is re-instantiated.


Back Navigation

You should ensure that pressing the Back button on a detail screen returns the user to the master screen. To do so, call addToBackStack() before you commit the transaction:

getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack(null)
.commit();

When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).


Fragments seems to be pretty easy at first but there is a lot more to it. You need to take care of lots of things while using fragments such as memory, navigation, callbacks, bundle etc. I hope most commonly faced issues and most commonly made mistakes covered in this article.

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

amod kanthe

Written by

Mobile lead @ TravelX.ai and Scandid.in, Technical writer, Game Programmer https://in.linkedin.com/in/amod-kanthe-61a12a43

MindOrks

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade