pdfview

How to restore the last page was loaded in a PDF View?


I'm a beginner developer, and it’s my first question post ever.

My app presents a PDF e-book, I used this library:

com.github.barteksc:android-pdf-viewer:2.8.2

When I reopen my app, pdfView loads the first page “pageNumber=0”, I want it to load the last page I left it.

I found most solutions were presented for this issue on SO, but nothing worked for me.

I think there is a simple solution, such as using:

SavedPage = pdfView.getCurrentPage() inside onSavedInstanceState, then restoring that integer inside onCreate.

But I don’t know how to do this. So it would be kind of you to guide me to the simplest way to make my pdfView load the last page I was reading.

Here is the code of my Reading Activity:

package com.turquoise.jeebalalhasanat;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.shockwave.pdfium.PdfDocument;
import java.util.List;

public class ReadingActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener {

    private static final String TAG = ReadingActivity.class.getSimpleName();
    public static final String PDF_FILE = "my_book.pdf";

    PDFView pdfView ;
    Integer pageNumber;
    String pdfFileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reading);

        pdfView = (PDFView)findViewById(R.id.pdfView);
        displayFromAsset(PDF_FILE);
    }

    private void displayFromAsset(String assetFileName) {
        pdfFileName = assetFileName;

        pdfView.fromAsset(PDF_FILE)
                .defaultPage(pageNumber)
                .enableSwipe(true)
                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
        }

    @Override
    public void onPageChanged(int page, int pageCount) {

        pageNumber = page;
        setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));

    }

    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pdfView.getDocumentMeta();
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

Any help is appreciated!


Solution

  • Thank God, I reached to a solution! Android Developer Documentation of Google will remain developer’s best friend.

    I would like to share it, to help every beginner developer who needs help about this issue.

    So here is how to restore the last page you load in a PDF View: First step: declare an Integer variable inside your activity:

    Integer savedPage;
    

    Second step: inside onStop():

    @Override
    protected void onStop() {
        super.onStop();
    
    
    savedPage = pdfView.getCurrentPage();
    
    \\ create a shared preference file
    
        SharedPreferences mySharedPreferences = getPreferences(Context.MODE_PRIVATE);
    
    
    \\ to write to this shared preference file
    
        SharedPreferences.Editor myEditor = mySharedPreferences.edit();
    
    
    
    \\ store the page number you left it 
    
        myEditor.putInt("retrievedPage",savedPage);
    
    
    
    \\ to save changes
    
     myEditor.apply();
    
    
    
    }
    
    

    Third step: inside onCreate():

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reading);
    
    
    \\ To retrieve the  number of the last page from your shared preferences file
    
     SharedPreferences mySharedPreferences = getPreferences(Context.MODE_PRIVATE);
    
    savedPage = mySharedPreferences.getInt("retrievedPage",0);
    
        pageNumber = savedPage;
    }
    
    

    Here is the full activity code:

    import android.os.Bundle;
    import android.util.Log;
    import com.github.barteksc.pdfviewer.PDFView;
    import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
    import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
    import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
    import com.shockwave.pdfium.PdfDocument;
    import java.util.List;
    
    
    
    
    
    
    public class ReadingActivity extends Activity  implements OnPageChangeListener,OnLoadCompleteListener {
    
    
        private static final String TAG = ReadingActivity.class.getSimpleName();
        public static final String PDF_FILE = "my_book.pdf";
    
    
        PDFView pdfView ;
        Integer pageNumber;
        String pdfFileName;
    
    Integer currentPageNumber;
    Integer savedPage;
    
    
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_reading);
    
    
    
    
    
    
    SharedPreferences mySharedPreferences = getPreferences(Context.MODE_PRIVATE);
    
    savedPage = mySharedPreferences.getInt("retrievedPage",0);
    
    pageNumber = savedPage;
    
    
    
    
    
    
    
            pdfView = (PDFView)findViewById(R.id.pdfView);
            displayFromAsset(PDF_FILE);
    
    
        }
    
    
    
    
    
    
    
    
    
    
        private void displayFromAsset(String assetFileName) {
            pdfFileName = assetFileName;
    
            pdfView.fromAsset(PDF_FILE)
                    .defaultPage(pageNumber)
                    .enableSwipe(true)
                    .swipeHorizontal(false)
                    .onPageChange(this)
                    .enableAnnotationRendering(true)
                    .onLoad(this)
                    .scrollHandle(new DefaultScrollHandle(this))
                    .load();
    
    
    
    
            }
    
    
    
    
    
        @Override
        public void onPageChanged(int page, int pageCount) {
    
            pageNumber = page;
            setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
    
                }
    
    
    
    
        @Override
        public void loadComplete(int nbPages) {
            PdfDocument.Meta meta = pdfView.getDocumentMeta();
            printBookmarksTree(pdfView.getTableOfContents(), "-");
    
        }
    
    
        public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
            for (PdfDocument.Bookmark b : tree) {
    
                Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
    
                if (b.hasChildren()) {
                    printBookmarksTree(b.getChildren(), sep + "-");
                }
            }
        }
    
    
    
    @Override
    protected void onStop() {
        super.onStop();
    
        SharedPreferences mySharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor myEditor = mySharedPreferences.edit();
    
    
    savedPage = pdfView.getCurrentPage();
    myEditor.putInt("retrievedPage",savedPage);
    myEditor.apply();
    
    
    }
    
    }
    

    Now you should achieve your goal. Happy coding ^^!