Saturday, July 2, 2011

Re: MVP best practices, how to expose a view to a presenter?

Keep a reference on the view in a field; and if you want to make sure you won't use the view after the activity is stopped, reset the field back to null in onCancel and onStop.

What I generally do is to inject/pass the view in the activity's constructor and store it in a field; and only setPresenter from the start method (like you did), and setPresenter(null) from onStop and onCancel (to make sure the view doesn't keep a reference on the activity once that one is stopped). That way, I can easily decide the lifetime of the view: singleton, "cached for a while", or created each time a new activity is needed. By injecting in the activity's constructor though, I assume that the activity's lifetime is shorter than the one of the view (my activities are all disposable: once they're stopped or cancelled, it's an error to try to start them again, my code should create a new activity), YMMV.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/eE2czJX06MoJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment