androidmozillageckoview

How can I save & restore GeckoView's session state?


I would like to keep the session active between orientation changes. Geckoview's documentation is lacking a bit in this area. It seems GeckoSession has a restoreState method that accepts a SessionState. Is there a way to create the SessionState object from the current state of the session?

Another approach I have tried was to create a Parcelable object and set this as the SessionState for restoreState but this didnt work.


Solution

  • You can keep track of the current state of a GeckoSession installing a ProgressDelegate and implementing onSessionStateChange, something like:

    class ProgressDelegate extends GeckoSession.ProgressDelegate {
      @Override
      public void onSessionStateChange(GeckoSession session,
                                       SessionState sessionState) {
        // store sessionState somewhere
      }
    }
    
    ProgressDelegate delegate = new ProgressDelegate();
    session.setProgressDelegate(delegate);