Sunday, February 10, 2013

Re: Activity and EventHandler confusion

1. I understand that Activity instances are disposable but view instances are not. 

2. As a result of a place change, my ActivityMapper creates a new Activity instance:
           if (place instanceof LoginPlace)
              return new LoginActivity((LoginPlace) place, clientFactory);

Question: While it is true that Activity is supposed to be disposable, is it really a good idea to create a new instance instead of reusing an existing one? I.e. is it better to just do clientFactory.getHomeActivity()?

Activities are lightweight compared to widget/views because widget/views need to do DOM operations to construct themselves. Most people recreate activities so they dont have to care about their internal state. You always have a clean new instance and the old instance can be garbage collected.

Views/Widget on the other hand my be cached/singleton if its worth it. For example if you have an endless scrolling list you dont want to create list entry widgets all the time. If you only see 20 entries at any time you maybe have a list of 30 entry widgets that are reused while scrolling.

 

Now, as per the JavaDoc documentation of Activity, 
Any handlers attached to the provided event bus will be de-registered when the activity is stopped, so activities will rarely need to hold on to the HandlerRegistration instances returned byEventBus.addHandler(com.google.gwt.event.shared.GwtEvent.Type, H).
So, what I understand is that in the mayStop() method of my activity, I don't have to do anything wrt the event handler. If the user goes back to the same activity again, a new Activity instance will be created and a new handler will be registered (since that code is in the constructor). The old registration is automatically cleaned up.

The handler is only cleaned up automatically if you use the "special" EventBus provided in Activity.start(). The EventBus in the start() method is your application wide EventBus (the one you gave the ActivityManager) wrapped by a ResettableEventBus. Whenever you switch Activities the ActivityManager will reset the ResettableEventBus which causes all handlers being added by the activity to be deregistered.


-- J.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment