Search code examples
androidperformancemvpandroid-lifecycle

What's the best place to allocate your controller class in the Android lifecycle?


i use in my android projects the MVP pattern. and therefore allocate the presenter in my activity that i am going to use. in android there are 3 stages before the activity is completely active. the onCreate, onStart and onResume, but what is the best place for allocating the Presenter class for optimal UI and Memory performance? and the Presenter class should not leak memory.


Solution

  • Most of the time you should do it in onCreate callback. Everything should be instantiated here. Normally there is no reason to do it elsewhere, because you have the same presenter regardless of the Activity state. In some special cases, if you have to bind and unbind some kind of lister that your activity doesn't listen to events while in paused or stopped state, you can work with onResume and onPause but again, that just special cases. And most of the time you just call some methods on your already created presenter.