skip to Main Content

I am currently developing a pdf reader app and I want the app to display the current reading page Incase the App is closed and reopened again.
I’m using this dependency
implementation ‘com.github.barteksc:android-pdf-viewer:3.2.0-beta.1’

2

Answers


  1. Here is Kotlin’s help, Save your page number in SharedPreferences in onDestroy(). When you open your app Then get the page number from SharedPreferences and load pdf.

    MainActivity.kt

     class MainActivity : AppCompatActivity() {
        private var defaultPage = 0
         override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                defaultPage = getPageNumber() // get value form SharedPreferences
                loadPdfViewer()
        
            }
        
        private fun loadPdfViewer() {
                pdfView.fromFile(pdfPath)
                    .enableSwipe(true) // allows to block changing pages using swipe
                    .swipeHorizontal(false)
                    .enableDoubletap(true)
                    .defaultPage(defaultPage)
                    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                    // spacing between pages in dp. To define spacing color, set view background
                    .spacing(5)
                    .onError {
                        // do something
                    }
                    .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
                    .pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
                    .nightMode(false) // toggle night mode
                    .load()
        
            }
        
           override fun onDestroy() {
                super.onDestroy()
                defaultPage = pdfView.currentPage
                // save this defaultPage number in SharedPreferences
                setPageNumber(defaultPage )
            }
    
    fun getPageNumber(): Int {
            val sharedPreferences = getSharedPreferences("pageNumberPrefs", Context.MODE_PRIVATE)
            return sharedPreferences.getInt("pageNumberValue", 0)
        }
    
        fun setPageNumber(pageNumber: Int) {
            val sharedPreferences = getSharedPreferences("pageNumberPrefs", Context.MODE_PRIVATE)
            val sharedPreferencesEditor = sharedPreferences.edit()
            sharedPreferencesEditor.putInt("pageNumberValue", pageNumber)
            sharedPreferencesEditor.apply()
        }
        }
    
    Login or Signup to reply.
  2. You can do this with simple logic.

    Assuming this is an offline or online application. Each time you load a pdf page, you can get the page number each time the user opens a new page or goes back to previous page. You can store this page when the onDestroy() or the ‘onStop()’ method is called.

    You can store the page information using:

    1. SharedPreferences.
    2. Simple SQLite database.
    3. Remote database, if the application has remote/online backend.

    Each time the onResume() method is called, you can fetch the last page information and pass it to the pdf loader or WebView and it will load the page number. In case the page information cannot be retrieved, you can then load the first page of the pdf.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search