Find your content:

Search form

You are here

How does VF decide when to instantiate a new instance for a controller

 
Share

I am following the book Visualforce Developer's Guide. There is one thing I am not quite clear with: when multiple VF pages are using one single controller - like in a wizard, how do VF decide when to create a new controller class instance and when will it use a previous one?


Attribution to: Lance Shi

Possible Suggestion/Solution #1

According to visualforce guide > pageReference > setRedirect https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_PageReference_setRedirect.htm:

If setRedirect(false), the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

In other words, if you want the controller to be reinstantiated (and hence the constructor reinvoked), in the pageReference method, use pgRef.setRedirect(true). The next page is loaded with the whole controller reinstantiated via its constructor.

But, if you don't want the controller class to be reinstantiated, use pgRef.setRedirect(false). the next page is loaded without re-construction.


Attribution to: Cray Kao
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32252

My Block Status

My Block Content