CDIView Lifecycle

Hello,

as far as I have developed my application so far, the CDIAddon of Vaadin is a huge advantage to me. Considering the behaviour of my application, the lifecycle of every CDIView equals the lifecycle of the UI.

Is there any way to change that? Any View should only be instanced if needed (wheras “need” is defined by calling the view via navigateTo() ) and should be destroyed after navigating to
another
view.

Unless you have injected instances of your Views somewhere in the UI or other Views , they will already be instanced only if needed.

Destroying an instance of your View as you navigate away might be a bit more tricky…

See
http://stackoverflow.com/questions/8420070/how-to-create-and-destroy-cdi-weld-managed-beans-via-the-beanmanager

You could modify the CDIAddon to listen to a CDI navigation Event and remove bean instance from BeanManager , however a Custom CreationalContext that is aware of the Navigation state is probably more appropriate.

Hi,

I’m trying to do it the following way:

I map every CreationalContext instance to the view name it belongs to. When navigateTo() is called, the ViewProvider releases the context and destroys the corresponding bean.

public void destructViewBean(Class<? extends View> view) {

        String viewName = view.getAnnotation(CDIView.class).value();

        Bean<View> d = (Bean<View>) getViewBean(viewName);

        View reference = (View) beanManager.getReference(d, View.class, viewCreationalContexts.get(viewName));

        d.destroy(reference, (CreationalContext<View>) viewCreationalContexts.get(viewName));

        viewCreationalContexts.get(viewName).release();
}

However, the beans are still ‘living’. What am I doing wrong here?

For everyone who has the same problem:

Upate to the newest version of the CDI Addon and annotate views with “@NormalViewScoped”.