Reputation: 4593
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?
Upvotes: 2
Views: 23488
Reputation: 5260
1) yes it can be better
2) yes you are correct, as event listener is initialised there than the process will be repeated 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.
Upvotes: 10
Reputation: 2114
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.
Upvotes: 4