Toolbar to each fragment causing memory issue
Asked Answered
I

3

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.

Interdepartmental answered 9/3, 2018 at 17:42 Comment(7)
Why do you have Toolbar for each fragment. Activity should have Toolbar.Tripod
They are collapsing toolbar, at few places toolbar is custom so kept in fragmentInterdepartmental
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.Tripod
but collapsing toolbar is transparent and other screens have custom views in toolbar which is difficultInterdepartmental
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.Divert
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.Steed
Just remove toolbar in onDetach() method of fragment before calling it super method of onDetach()Cabbala
D
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.

Divert answered 10/3, 2018 at 10:26 Comment(1)
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.Mahan
T
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().

Tanta answered 18/3, 2018 at 6:24 Comment(1)
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.Mahan
B
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

Barnabas answered 14/3, 2018 at 10:39 Comment(1)
"above answer" is not clear, there are multiple ways answers can be ordered. Use a link to indicate what answer you mean.Mcdonnell

© 2022 - 2024 — McMap. All rights reserved.