As i understand it, would it not be better to create a fragments event listeners in the fragments onCreate? If it is done in onCreateView it would have to be redone everytime the fragment comes back into view (onResume())? Would it make a difference where it is set?

有帮助吗?

解决方案 2

An event is set on a view and hence is restored when the view is restored. For example, lets say we set a listener on a button in a fragment that is created in onCreateView. If at some point of time, if the fragment is restored (without a call to onCreateView), the button is restored along with the listener that is set. The same goes for setting a text/background for a button - you don't have to reset the text/background each time the fragment is resumed.

其他提示

1) yes it can be better

2) yes you are correct, as event listener is initialised there than the process will be repeated here

enter image description here

3) please have a close look at life-cycle method

4)it always makes difference where you are using and when you are initializing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top