CDI getting the instance of a view in Platform 14

Hi,

On my framework 8 CDI project I obtained the instance of a view/context using the com.vaadin.cdi.CDIViewProvider by injecting the view provider to my UI like this.

    @Inject
    private CDIViewProvider viewProvider;

After that you got the view instance by using viewProvider.getView("name-of-view").

I’m trying to accomplish the same thing on Platform 14. For this, the class for which I want to get the instance of is like this.

@Route(value = "country-view", layout = MainView.class)
@PageTitle("Countries")
@RouteScoped
public class CountryView extends AbstractFormLayoutView<Country> {
   //...
}

The documentation for Platform 14 suggests to use the BeanProvider (in Deltaspike). When I try to get it, I get a valid Bean, but the context is null. I’m doing this on my main layout after construction.

@Route("")
@Theme(value = Lumo.class)
public class MainLayout extends AppLayout {

    @Inject
    private GreetService greetService;

    @Inject
    private MongoDBDataConnection mongoDBConn;

    @PostConstruct
    public void init() {

     // CODE TO OBTAIN INSTANCE GOES HERE
	 
	 //ALTERNATIVE 1
	 
	 //This returns a valid Bean with the correct class
	 Bean<?> bean = CDI.current().getBeanManager().getBeans(CountryView.class).stream().findFirst().get(); 
	 //Now to get the instance of the view
	 Context cntx = CDI.current().getBeanManager().getContext(RouteScoped.class);
	 Object instance = cntx.get(bean); //This returns null 
	 
	 //ALTERNATIVE 2
	 Object obj = BeanProvider.getContextualReference("country-view", true); // THIS RETURNS NULL
    }

Any ideas or alternatives that work are welcome!

Thanks.

I was able to solve this using

MyClass myClass = BeanProvider.getContextualReference(MyClass.class, false);

This gives you the instance of the view/route/context you are trying to access.