title: How to execute code in the onSuccess of a page-switching AsyncCallback
body: <p>New to GWT, oh! this learning curve...</p>
  
  <p>So, I'm testing out the authentication functionality I've implemented: When querying for the current user's role through an <code>AsyncCallback</code>, a check is first made to see whether the user is logged in or not. This is done in my own implementation of an <code>AsyncCallback</code> (see <a href="https://stackoverflow.com/questions/3657572/how-to-redirect-to-login-page-after-session-expire-in-gwt-rpc-call">How to redirect to login page after session expire in GWT RPC call</a>). If found to be not logged in, the following is happening:</p>
  
  <pre><code>Widget returnToWidget = RootPanel.get(MyApp.Default_RootPanel_Id).getWidget(0);
  RootPanel.get().add(new LoginPage(returnToWidget));
  </code></pre>
  
  <p>and the <code>LoginPage</code> clears the <code>RootPanel</code>, adds a dummy <code>FlowPanel</code> and pops up a <code>DialogBox</code> with username/password/submit widgets. The <code>ClickListener</code> added to the submit widget does the following if credentials are accepted:</p>
  
  <pre><code>@Override
  public void onSuccess() {
      RootPanel.get().clear();
      RootPanel.get().add(returnToWidget);
  </code></pre>
  
  <p>Now, this is working just fine. To address the actual problem, please allow me to get back to the query call, which is taking place in a <code>MainPanel extends Composite</code>:</p>
  
  <pre><code>public MainPanel() {
      final DockPanel dockPanel = new DockPanel();
      initWidget(dockPanel);
      final HorizontalPanel hPanel = new HorizontalPanel();
      hPanel.add(new Button("TEST"));
  
      authService.getUserRole(jSessionId, new AsyncCallback&lt;Constants.UserRole&gt;() {
          @Override
          public void onSuccess(UserRole currentUserRole) {
              boolean isAdmin = currentUserRole.isAtLeast(Constants.UserRole.Administrator);
              if (isAdmin) {
                  hPanel.add(createNewStudyButton);
              }
              hPanel.add(new Button("ButtonForAll"));
              System.out.println("CODE EXECUTED!");
          [...]
          }
      [...]
  
      dockPanel.add(hPanel, DockPanel.NORTH);
      System.out.println("THROUGH!");
  }
  </code></pre>
  
  <p>What is happening is that when starting my GWT app, <code>MainPanel</code> is instantiated in the EntryPoint and added to <code>RootPanel</code>, resulting in the console printing <em>THROUGH!</em>, the login screen being shown and after providing accepted admin credentials, the passed instance of <code>MainPanel</code> is added to the <code>RootPane</code>.</p>
  
  <p>Unfortunately (and here's the problem!), only the TEST button is shown. The console never prints <em>CODE EXECUTED!</em>. How come the code within the <code>@success</code> response is not being executed? When refreshing the page, the code is executed and all buttons are shown, as the <code>authService.getUserRole</code> now returns a result without an interruption from the login screen.</p>
  
  <p>I'd really, really like a <code>repaint()</code> or something equivalent, but the problem seems to be more of a design issue.</p>
  
  <p>Can anyone please help me getting the code block executed even though the <code>AsyncCallback</code> does a page switch before returning its result?</p>
  
  <p>UPDATE:</p>
  
  <p>My bad! Problem found, but not solved, as I don't think it's solvable within this design:</p>
  
  <p>My SecurityAsyncCallback has a reference to the default AsyncCallback and on <code>onSuccess(T result)</code>, it simply passes the the result to the default AsyncCallback, so it reaches the actual caller. My design for handling <code>NotLoggedInException</code> thrown server side on calling e.g. <code>authService.getUserRole</code> is to catch it in my <code>SecurityAsyncCallback</code>'s <code>onFailure</code> and show the login screen... but from here I cannot re-run the code that resulted in the <code>onFailure</code>, so that the caller will experience an <code>onSuccess</code>, as the user is now logged in; the <code>AsyncCallback</code> interface is just an intermediary for passing <em>results</em> of the call between caller and callee and has no knowledge of what has been called.</p>
  
  <p>Well, what I'm really after is that when user has digged himself deep into my app, leaves his computer and his login times out, I want the login page to be shown on next server side access and then redirect him back to where he came from (that is, not back to the default page after logging in). I guess I can achieve this through the use of the History subsystem, but I haven't yet tried this.</p>