[SalesForce] How to persistent data between VF pages (that change the view state) that share one controller

I have multiple VF pages that share one controller. My problem is that:

1.) If I set PageReference.setRedirect(false), it doesn't redirect the pages

2.) If I set PageReference.setRedirect(true), it invokes a new controller whenever we get to the redirected page so data does not persist

Should I move all my VF pages into one page and have the divs show/hide the different pages so I don't have to change the view state? I'm not sure how else I can both pass data and change the view state otherwise

Best Answer

Just an example Hope this will help you

public class SearchController1 {

    public List<Account> accounts {get; set;}

    public SearchController1() {
        this.accounts = new List<Account>();
        checkCurrentPage();
    }
    public void checkCurrentPage() {
         system.debug('====check which page we are on===='+system.currentPageReference());
    }
    public void doSearch() {
        this.accounts = [select Id,Name from Account limit 200];
    }

    public PageReference doClick() {
        return Page.ViewPage2.setRedirect(false);
    }
}

ViewPage1 VF page

<apex:page controller="SearchController1" title="SearchPage" showHeader="true" sidebar="false" id="page">
    <apex:form id="form">
        <apex:pageBlock id="block">
            <apex:pageBlockButtons >
                <apex:commandButton value=" Search!! " title=" Search!! " action="{!doSearch}" reRender="form" />
                <apex:commandButton value=" Click!! " title=" Click!! " action="{!doClick}" rendered="{!accounts.size > 0}" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accounts}" var="item">
                <apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
                    <apex:outputText value="{!item.Name}" />
                </apex:column> 
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

ViewPage2 VF Page

<apex:page controller="SearchController1" title="ViewPage" showHeader="false" sidebar="false" id="page" action="{!checkCurrentPage}">
    <apex:form id="form">
        <apex:pageBlock id="block">
            <apex:pageBlockTable value="{!accounts}" var="item">
                <apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
                    <apex:outputText value="{!item.Name}" />
                </apex:column> 
            </apex:pageBlockTable>
        </apex:pageBlock>
     </apex:form>
</apex:page>

Chech checkCurrentPage method for which page we are on.

So when you set redirect = false. Onclick of Click!! button It will redirect to ViewPage2. In this page we are using same list account fetched in ViewPage1.

If you see browser top url is changed and still pointing to ViewPage1 but believe me It is redirect to viewPage2.

Try to create fresh page and controller.

Good Luck..

Note: Whenever we use setredirect = true false, URL doesn't change. so don't confused


Edit

If setRedirect = true

A redirect is performed through a client side redirect. This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST

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.


Note that if the URL of the PageReference object is set to a website outside of the salesforce.comdomain, or to a page with a different controller or controller extension, the redirect always occurs, regardless of whether the redirect attribute is set to true or false.