private void performActionForState(int forState) {
   if (!fragment.isAdded()) {
     return;
   }
   switch (forState) {
     case STATE_NO_FETCH:
       activity.updateProgressBar(true, true, 0);
       // hide the lead image...
       leadImagesHandler.hide();
       bottomContentHandler.hide();
       activity.getSearchBarHideHandler().setFadeEnabled(false);
       new LeadSectionFetchTask(pageSequenceNum).execute();
       break;
     case STATE_INITIAL_FETCH:
       new RestSectionsFetchTask(pageSequenceNum).execute();
       break;
     case STATE_COMPLETE_FETCH:
       editHandler.setPage(model.getPage());
       // kick off the lead image layout
       leadImagesHandler.beginLayout(
           new LeadImagesHandler.OnLeadImageLayoutListener() {
             @Override
             public void onLayoutComplete() {
               if (!fragment.isAdded()) {
                 return;
               }
               searchBarHideHandler.setFadeEnabled(leadImagesHandler.isLeadImageEnabled());
               // when the lead image layout is complete, load the lead section and
               // the other sections into the webview.
               displayLeadSection();
               displayNonLeadSection(1);
             }
           });
       break;
     default:
       // This should never happen
       throw new RuntimeException("Unknown state encountered " + state);
   }
 }
  private void loadPageOnWebViewReady(boolean tryFromCache) {
    // stage any section-specific link target from the title, since the title may be
    // replaced (normalized)
    sectionTargetFromTitle = model.getTitle().getFragment();

    Utils.setupDirectionality(
        model.getTitle().getSite().getLanguageCode(), Locale.getDefault().getLanguage(), bridge);

    // hide the native top and bottom components...
    leadImagesHandler.hide();
    bottomContentHandler.hide();
    bottomContentHandler.setTitle(model.getTitle());

    if (model.getCurEntry().getSource() == HistoryEntry.SOURCE_SAVED_PAGE) {
      state = STATE_NO_FETCH;
      loadSavedPage();
    } else if (tryFromCache) {
      // is this page in cache??
      app.getPageCache()
          .get(
              model.getTitleOriginal(),
              pageSequenceNum,
              new PageCache.CacheGetListener() {
                @Override
                public void onGetComplete(Page page, int sequence) {
                  if (sequence != pageSequenceNum) {
                    return;
                  }
                  if (page != null) {
                    Log.d(
                        TAG, "Using page from cache: " + model.getTitleOriginal().getDisplayText());
                    model.setPage(page);
                    model.setTitle(page.getTitle());
                    // load the current title's thumbnail from sqlite
                    updateThumbnail(
                        PageImage.PERSISTENCE_HELPER.getImageUrlForTitle(app, model.getTitle()));
                    // Save history entry...
                    new SaveHistoryTask(model.getCurEntry(), app).execute();
                    // don't re-cache the page after loading.
                    cacheOnComplete = false;
                    state = STATE_COMPLETE_FETCH;
                    setState(state);
                    performActionForState(state);
                  } else {
                    // page isn't in cache, so fetch it from the network...
                    loadPageFromNetwork();
                  }
                }

                @Override
                public void onGetError(Throwable e, int sequence) {
                  Log.e(TAG, "Failed to get page from cache.", e);
                  if (sequence != pageSequenceNum) {
                    return;
                  }
                  // something failed when loading it from cache, so fetch it from network...
                  loadPageFromNetwork();
                }
              });
    } else {
      loadPageFromNetwork();
    }
  }