Is there a way to call a method upon leaving a page with JSF?
If you use omnifaces @ViewScoped annotation in the backing bean the bean object is destroyed when you leave the view; so you can call a function when this happens using the @PreDestroy annotation in it.
Note: You must use omnifaces @ViewScoped annotation; with standard JSF @ViewScoped annotation the object isn't destroyed just by leaving the view, so pay attention to the import!
Source: http://showcase.omnifaces.org/cdi/ViewScoped