I'm trying to save an Intent object to SharedPreferences and the result code from onActivityResult() so I can use it later. I have already tired to store it as a json using Gson, but there is data loss. I have also tried to save it as uri, but the Intent.toURi() method returns empty string or a string that is not complete. Is there any other way to save it than as a string ? The intent has extras and I need to save all of its data, not just some parts of it.
This is what I've tried for storing it as uri:
SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
SharedPreferences.Editor editor = settings.edit();
String uriString = data.toUri(0);
editor.putString("SavedIntentData", uriString);
editor.putInt("SavedResultCode", resultCode);
editor.apply();
and for restoring it:
SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
String intentData = settings.getString("SavedIntentData", null);
int resultCode = settings.getInt("SavedResultCode", 0);
if(resultCode !=0 && intentData != null) {
data = Intent.parseUri(intentData, 0);
}
And for json:
String intentData = new Gson().toJson(data);
to restore it:
data= new Gson().fromJson(intentData, Intent.class);