I made an Android app that has a lot of pdf serially in a listview. Now I want to give a feature to the user that the user can view his/her last viewed PDF with a Floating Action Button. This FAB will open the PDF that was opened/studied at last by the user. But how can I do that? Thanks in Advance. Any help Appreciated. Now I will add my code here -
public static final String mypreference = "mypref";
public static final String PDF = "pdfKey";
String a = "One";
String b = "Two";
String c = "Three";
String d = "Four";
String e = "Five";
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(PDF)) {
}
if (list.isItemChecked(0)){
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(PDF)) {
Intent intent = new Intent(getApplicationContext(), c2017p1.class);
startActivity(intent);
}
}
else if (list.isItemChecked(1)){
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(b)) {
Intent intent = new Intent(getApplicationContext(), c2017p2.class);
startActivity(intent);
}
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
if (position == 0) {
Intent intent = new Intent(getApplicationContext(), c2017p1.class);
startActivity(intent);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PDF, a);
editor.commit();
SharedPreferences preferences = getSharedPreferences(PDF, 0);
preferences.edit().remove(b).apply();
SharedPreferences preferences1 = getSharedPreferences(PDF, 0);
preferences1.edit().remove(c).apply();
SharedPreferences preferences2 = getSharedPreferences(PDF, 0);
preferences2.edit().remove(d).apply();
SharedPreferences preferences3 = getSharedPreferences(PDF, 0);
preferences3.edit().remove(e).apply();
}
if (position == 1) {
Intent intent = new Intent(getApplicationContext(), c2017p2.class);
startActivity(intent);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PDF, b);
editor.commit();
SharedPreferences preferences = getSharedPreferences(PDF, 0);
preferences.edit().remove(a).apply();
SharedPreferences preferences1 = getSharedPreferences(PDF, 0);
preferences1.edit().remove(c).apply();
SharedPreferences preferences2 = getSharedPreferences(PDF, 0);
preferences2.edit().remove(d).apply();
SharedPreferences preferences3 = getSharedPreferences(PDF, 0);
preferences3.edit().remove(e).apply();
}
Now I found out that to listen that if a item is clicked, we have to use list.isItemChecked(position). But I still can not decide how to open this last viewed PDF from this? I want to store the Index of Shared Preferences of the last Viewed PDF and delete other one indexs. Then after clicking on the Floating Action Button, it will detect the Index of Last Viewed PDF as it will store only Last ones, and open the last one. Please Help me guys.