Is it better to set listeners in onViewCreated or onStart? (Fragments)

时间:2018-02-03 10:09:31

标签: android

Should I set listeners in my onCreate(View) methods or in onStart and when setting them in onStart, should I remove them again in the onPause method? Is there any downside to do it in onCreate(View) and not remove them? Should I differ between View listeners and other listeners?

EDIT: I looks like it is better to use onViewCreated() instead of onCreateView() for listeners. Still, is it better to use onViewCreated() or onStart()? Maybe it is more important to know if it is necessary to remove listeners or not.

EDIT: I looked into stuff about memory leaks and it looks like it's ok to use the listeners in onViewCreated() but I didn't find any official solution or suggestion The following links helped me about this problem

Do listeners create memory leaks if not removed from a destroyed activity?

https://android-developers.googleblog.com/2009/01/avoiding-memory-leaks.html

1 个答案:

答案 0 :(得分:0)

onViewCreated() is the best place to set all listeners and if you talk about on onStart() you can't get any view so it will throw NullPointerException.