I
I
iVARIOUS2017-09-26 12:33:34
Android
iVARIOUS, 2017-09-26 12:33:34

Static Handler in activity causes memory leak?

There is a main activity, in it the main fragment in which several more fragments change in turn. When trying to call a method in a parent fragment using getParentFragment() from its child fragments - throws an error. The documentation says that if the fragment is attached to the activity, then the method returns null. And I decided to create a static Handler to run methods in the main activity, from its child fragment, and a non-static Handler to run the methods of this fragment from its lower child fragments. Everything works, but does it lead to a memory leak? Activity never closes (no transitions between activities), only fragments in this activity change

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2017-09-26
@red-barbarian

if MainActivity
should not if onCreate handler replace.
You can also make handler = null in onDestroy
In general, check. At least monitor the memory when you rotate the screen.

D
Denis Zagaevsky, 2017-09-27
@zagayevskiy

...the main fragment in which several more fragments change in turn. When trying to call a method in a parent fragment using getParentFragment() from its child fragments - throws an error.

If they are really "in which", then there should be no mistake. Are you using getChildFragmentManager() to add child fragments?
I did not understand why the handler is for calling the activation methods. He's not needed. An activity can be retrieved by calling getActivity() on a fragment.
To tell about a leak, you need to see the code. Most likely it is. Telepaths can answer more precisely, but they are on vacation.

I
iVARIOUS, 2017-09-27
@iVARIOUS

With getActivity () - it is clear. It works and the methods from the child fragment are called. But this child fragment has its own child fragments which are already throwing an error on getParentFragment() since it works in the activity. And in order not to get confused in the ways of calling parent functions, I decided to make one approach for everything (that is, static handlers that are visible from afar, and to which messengers can be sent). I read in some article that static handlers have implicit links to activities and fragments, but these links are called "weak", so they are removed by the garbage collector and do not lead to leaks. In the application manager of the phone, with a program weight of 5 mb, sometimes a weight of 20-40 mb is displayed there, and I don’t understand what this means. Image processing application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question