MVP pattern & problem with singletons views

227 views
Skip to first unread message

Juan Pablo Gardella

unread,
Jun 12, 2011, 3:31:23 PM6/12/11
to google-we...@googlegroups.com
Hi,

Recently I have a problem and discover that the cause is about presenters (activities non singletons) and views singleton. My code is similar to this:

private void bind() {
myView.getButton1().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
...
});
myView.getButton2().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
...
});
}

This method is invoque in start() method of the Activity.


BUT have a problem this code. Add multiple handlers (one each time an activity start). Then, when I click the botton, the event execute n times (n times execute start method in activity). So I think this bind() method is dangerous, the presenter must know if the view is singleton or not.

There are a manner to use bind() method in activity and singletons view?

Juan

Juan Pablo Gardella

unread,
Jun 12, 2011, 3:50:44 PM6/12/11
to Google Web Toolkit
I can fix with static variables to store like this:

//static variables
private static HandlerRegistration handlerBoton1;
,,,

start(..){
...
bind();
}
...
bind() method:

//Esto lo hacemos para borrar el handler el presenter anterior.
if (handlerBoton1!=null)
handlerBoton1.removeHandler();

handlerBoton1= vista.getBoton1().addClickHandler(
new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
...
}
});


I don't like much, but it works. Is there are other way? Use eventBus
and @UiHandler don't have this problem, but I must have event
+eventHandler.

Juan


On 12 jun, 12:31, Juan Pablo Gardella <gardellajuanpa...@gmail.com>
wrote:

Raphael André Bauer

unread,
Jun 12, 2011, 3:55:14 PM6/12/11
to google-we...@googlegroups.com
On Sun, Jun 12, 2011 at 9:50 PM, Juan Pablo Gardella
<gardella...@gmail.com> wrote:
> I can fix with static variables to store like this:


There is an onStop() method in activities that is called when
activities are stopped... call a unbind() there and everything is nice
and clean..

Cheers,


Raphael

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

Juan Pablo Gardella

unread,
Jun 12, 2011, 4:20:54 PM6/12/11
to google-we...@googlegroups.com
Thanks Raphael!! I test this and works!! (and find a bug in my code, at first didn't work becouse bind() method there is in constructor, not in start)

In this way I can remove static variable.

Thanks a lot
Juan
 
2011/6/12 Raphael André Bauer <raphael.a...@gmail.com>

Thomas Broyer

unread,
Jun 12, 2011, 6:47:21 PM6/12/11
to google-we...@googlegroups.com
Don't forget the onCancel() method too: if your activity starts asynchronously, it can be cancelled before you set the view in the AcceptsOneWidget passed in argument to the start() method; in this case, onCancel() is called rather than onStop().

You'll find out that it's much easier to handle using a "presenter interface" to talk from your view back to your presenter (see http://code.google.com/webtoolkit/articles/mvp-architecture-2.html and http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html ): all your activity has to do is getView().setPresenter(this) in start(), and getView().setPresenter(null) in onStop() and onCancel(). You can use @UiHandlers in your view, and if you don't, you don't have to manage "event handler's lifecycle" either.

Juan Pablo Gardella

unread,
Jun 12, 2011, 7:55:02 PM6/12/11
to Google Web Toolkit
Hi Thomas!!

Thanks for the info. I fixed my code.

Juan

On 12 jun, 15:47, Thomas Broyer <t.bro...@gmail.com> wrote:
> Don't forget the onCancel() method too: if your activity starts
> asynchronously, it can be cancelled before you set the view in the
> AcceptsOneWidget passed in argument to the start() method; in this case,
> onCancel() is called rather than onStop().
>
> You'll find out that it's much easier to handle using a "presenter
> interface" to talk from your view back to your presenter (seehttp://code.google.com/webtoolkit/articles/mvp-architecture-2.htmlandhttp://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAnd...):
Reply all
Reply to author
Forward
0 new messages