I am trying to save the history of videos watched in my app but i am unable to retrieve the order in which the videos were watched , the order is all messed up here is the code i am using to save the arraylist
public void SaveHistory(Context context,String url,boolean save)
{
FetchHistory(context);
if(save)
{
pathList.add(url);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
LinkedHashSet<String> set = new LinkedHashSet<>(pathList);
sharedPreferences.edit().putStringSet("History",set).apply();
}
else
{
pathList.remove(url);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
LinkedHashSet<String> set = new LinkedHashSet<>(pathList);
sharedPreferences.edit().putStringSet("History",set).apply();
}
}
and this is the code i am using to retrieve array list
public ArrayList<String> FetchHistory(Context context)
{
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> newSet = sharedPreferences.getStringSet("History",null);
if(newSet!=null)
{
pathList.clear();
pathList.addAll(newSet);
Collections.reverse(pathList);
}
return pathList;
}