I am building an android app which displaying a specific pdf through https://github.com/barteksc/AndroidPdfViewer with lots of pages, and now i was just implementing a method to re-open the pdf from the last opened page of the user.
I am using SharedPreferences to store the current page, and then after reloading the app, the app will re-open the pdf where user left.
Here is my Shared Preferences method to store and retrieve the data
private void storepreferences () {
PDFView pdfView = findViewById(R.id.pdfView);
savedpage=pdfView.getCurrentPage();
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("key_name2", savedpage);
editor.apply();
}
private void getpreferences () {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
pageNumber = pref.getInt("key_name2", 0); // getting Integer
}
And then i am using onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
storepreferences();
getpreferences();
Also for a test i am displaying the
TextView txt = findViewById(R.id.textView2);
txt.setText(String.valueOf(pageNumber));
But Still i am getting the default value, What i am doing wrong can anyone tell me?