5

I have given toolbar for each fragment in my app.

Following is code in the fragment to set toolbar. setToolbar is a method in Activity which is called from fragment using the interface.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Toolbar toolbar = view.findViewById(R.id.toolbar);
    if (mListener != null) {
        mListener.setToolbar(toolbar);
    }
}

Now since I am not removing toolbar when the fragment is destroyed it is causing a memory leak. I want to know where should I remove the toolbar fragment and how.

Any idea where and how should I release toolbar which is in the fragment?

As per my previously asked question Can I have toolbar for each fragment separately. How to handle navigation drawer I was told I can have a toolbar in each fragment but now I am facing memory leak.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
  • 1
    Why do you have Toolbar for each fragment. Activity should have Toolbar. – Thracian Mar 09 '18 at 17:57
  • They are collapsing toolbar, at few places toolbar is custom so kept in fragment – amodkanthe Mar 09 '18 at 18:01
  • You can have a collapsing toolbar in activity layout and it's correct way to have toolbar in activity. Fragments have getActivity() method, you can cast activity to your activity. Define a method inside your activity and call it from fragment when needed and how it's needed. – Thracian Mar 09 '18 at 18:07
  • but collapsing toolbar is transparent and other screens have custom views in toolbar which is difficult – amodkanthe Mar 09 '18 at 18:17
  • From a design perspective, your toolbar should look similar all throughout the app, unless an activity/different is doing something vastly different from other activities. This will give the user a seamless experience. – MSS Mar 10 '18 at 10:31
  • One possible solution is to have another callback to set the toolbar to null onStop() in the fragment. That might help with the memory leak. – Eugene H Mar 16 '18 at 20:05
  • Just remove toolbar in onDetach() method of fragment before calling it super method of onDetach() – Amjad Khan Mar 18 '18 at 10:36

3 Answers3

5

Instead of creating toolbar for each fragment separately, create a single toolbar in the parent activity of those fragments.

If you are concerned about menu options in each fragment, then no need to worry. Just write setHasOptionsMenu(true) inside onCreateView method of each fragment. Also override onCreateOptionsMenu and onOptionsItemSelected in each fragment. Activity toolbar will reflect the changes in menu options automatically.

NOTE: Always generate an activity from the template provided by Android Studio. It will save you both time and energy. You can always remove all the boiler plate code which you deem to be unnecessary.

MSS
  • 3,306
  • 1
  • 19
  • 50
  • 2
    There are situations where seperate toolbars per fragment are useful - eg. diffrent implementation of ColapsingToolbar in each Fragment in Activity. So this answer provides no solution for that. – muminers Mar 18 '18 at 11:06
2

The solution is to not set the toolbar for the activity. But if you want to, you can remove it in the Fragment.onStop().

If your toolbars look the same (component-wise), Have the Toolbar in your activity, and upon onAttach() of each fragment, pass the arguments like toolbar title, hasBack and ... to your activity and let the activity handle showing it. This way you would never have a memory leak and also, everytime a fragment gets attached, the toolbar gets updated accordingly.

I suggest creating an interface like ToolbarInteractor and have two methods, setToolbar(title:String,hasBack:Boolean,...) and resetToolbar() and let your activity implement it. Then in your fragment, call ((ToolbarInteractor) getActivity()).setToolbar(...). Same goes for reset().

Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
  • This is the only one answer so far that is actually addressing the problem. I'd only add one thing - keeping reference to Toolbar in Fragment can leed to memory leaks so it's good practice to remove them at time. – muminers Mar 18 '18 at 11:12
0

Yes, as above answer you can have one parent activity in which you can have toolbar implementation and the fragments are implemented in the same. Now to customize your toolbar header you can implement a method interface and can use accordingly.

For brief & other option you may use this LINK

Mayank Nema
  • 223
  • 2
  • 7
  • "above answer" is not clear, there are multiple ways answers can be ordered. Use a link to indicate what answer you mean. – Nathan Hughes Mar 18 '18 at 12:40