Search code examples
androidandroid-recents

Save recent clicks by user


I needed to make a recently opened module for my app. I'm using a viewpager with 3 fragments, each one of those has a listview on it. The 1st listview that when clicked must store the clicked item on a listview on a different fragment. I was planning on using a different table, but i think that there is something much better, some sort of a temp memory.


Solution

  • It sounds like that you don't have a lot of data to save. You can consider using the SharedPreferences.

    Depending on what the item is in your listview, for example, if they are strings, you can store and retrieve them like this:

    Saving:

    string selectedItem = ...; 
    SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(this);
    manager.edit().putString("SelectedItem", selectedItem).apply();
    

    Retrieving:

    SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(this);
    String selectedItem = manager.getString("SelectedItem", "");