SharedPreferences doesn't store values after closing app












0















I tried to store some values in ArrayList by SharedPreferences by the following code:



public static String jsonSavePracticeResult;
private SharedPreferences sharedPreferencesSaveResults = PreferenceManager.getDefaultSharedPreferences(this);

public void savePracticeResults(ArrayList<KeepPracticeResultsModel>
arrayListPracticeResult) {
Gson gson = new Gson();
jsonSavePracticeResult = gson.toJson(arrayListPracticeResult);
SharedPreferences.Editor editorSharedPreferencesSaveResults;
editorSharedPreferencesSaveResults =
sharedPreferencesSaveResults.edit();
editorSharedPreferencesSaveResults.clear();
editorSharedPreferencesSaveResults
.putString("donePracticeList",jsonSavePracticeResult);
editorSharedPreferencesSaveResults.apply();
}


After that, I retrieved values by the following code:



public ArrayList<KeepPracticeResultsModel> getSavedPracticeResults(){

jsonSavePracticeResult = sharedPreferencesSaveResults.getString("donePracticeList", jsonSavePracticeResult);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<KeepPracticeResultsModel>>() {}.getType();
keepPracticeResultsModels = gson.fromJson(jsonSavePracticeResult, type);
return keepPracticeResultsModels;
}


When app is open, it works fine, but when you try to close app from recent apps, and after that you open app again, SharedPreferences values are lost and jsonSavePracticeResult is Null!
Also I called getSavedPracticeResults() in onResume() and onPause().



I did all of the solutions that existed in StackOverflow but it's happening again!










share|improve this question

























  • post some more code specially related to sharedPreferencesSaveResults

    – Omer
    Dec 28 '18 at 20:01











  • @Omer I added sharedPreferencesSaveResults in the code.

    – mhadikz
    Dec 29 '18 at 8:44











  • @RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

    – mhadikz
    Dec 29 '18 at 9:51











  • Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

    – Mohammadreza Khatami
    Dec 29 '18 at 18:47
















0















I tried to store some values in ArrayList by SharedPreferences by the following code:



public static String jsonSavePracticeResult;
private SharedPreferences sharedPreferencesSaveResults = PreferenceManager.getDefaultSharedPreferences(this);

public void savePracticeResults(ArrayList<KeepPracticeResultsModel>
arrayListPracticeResult) {
Gson gson = new Gson();
jsonSavePracticeResult = gson.toJson(arrayListPracticeResult);
SharedPreferences.Editor editorSharedPreferencesSaveResults;
editorSharedPreferencesSaveResults =
sharedPreferencesSaveResults.edit();
editorSharedPreferencesSaveResults.clear();
editorSharedPreferencesSaveResults
.putString("donePracticeList",jsonSavePracticeResult);
editorSharedPreferencesSaveResults.apply();
}


After that, I retrieved values by the following code:



public ArrayList<KeepPracticeResultsModel> getSavedPracticeResults(){

jsonSavePracticeResult = sharedPreferencesSaveResults.getString("donePracticeList", jsonSavePracticeResult);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<KeepPracticeResultsModel>>() {}.getType();
keepPracticeResultsModels = gson.fromJson(jsonSavePracticeResult, type);
return keepPracticeResultsModels;
}


When app is open, it works fine, but when you try to close app from recent apps, and after that you open app again, SharedPreferences values are lost and jsonSavePracticeResult is Null!
Also I called getSavedPracticeResults() in onResume() and onPause().



I did all of the solutions that existed in StackOverflow but it's happening again!










share|improve this question

























  • post some more code specially related to sharedPreferencesSaveResults

    – Omer
    Dec 28 '18 at 20:01











  • @Omer I added sharedPreferencesSaveResults in the code.

    – mhadikz
    Dec 29 '18 at 8:44











  • @RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

    – mhadikz
    Dec 29 '18 at 9:51











  • Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

    – Mohammadreza Khatami
    Dec 29 '18 at 18:47














0












0








0








I tried to store some values in ArrayList by SharedPreferences by the following code:



public static String jsonSavePracticeResult;
private SharedPreferences sharedPreferencesSaveResults = PreferenceManager.getDefaultSharedPreferences(this);

public void savePracticeResults(ArrayList<KeepPracticeResultsModel>
arrayListPracticeResult) {
Gson gson = new Gson();
jsonSavePracticeResult = gson.toJson(arrayListPracticeResult);
SharedPreferences.Editor editorSharedPreferencesSaveResults;
editorSharedPreferencesSaveResults =
sharedPreferencesSaveResults.edit();
editorSharedPreferencesSaveResults.clear();
editorSharedPreferencesSaveResults
.putString("donePracticeList",jsonSavePracticeResult);
editorSharedPreferencesSaveResults.apply();
}


After that, I retrieved values by the following code:



public ArrayList<KeepPracticeResultsModel> getSavedPracticeResults(){

jsonSavePracticeResult = sharedPreferencesSaveResults.getString("donePracticeList", jsonSavePracticeResult);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<KeepPracticeResultsModel>>() {}.getType();
keepPracticeResultsModels = gson.fromJson(jsonSavePracticeResult, type);
return keepPracticeResultsModels;
}


When app is open, it works fine, but when you try to close app from recent apps, and after that you open app again, SharedPreferences values are lost and jsonSavePracticeResult is Null!
Also I called getSavedPracticeResults() in onResume() and onPause().



I did all of the solutions that existed in StackOverflow but it's happening again!










share|improve this question
















I tried to store some values in ArrayList by SharedPreferences by the following code:



public static String jsonSavePracticeResult;
private SharedPreferences sharedPreferencesSaveResults = PreferenceManager.getDefaultSharedPreferences(this);

public void savePracticeResults(ArrayList<KeepPracticeResultsModel>
arrayListPracticeResult) {
Gson gson = new Gson();
jsonSavePracticeResult = gson.toJson(arrayListPracticeResult);
SharedPreferences.Editor editorSharedPreferencesSaveResults;
editorSharedPreferencesSaveResults =
sharedPreferencesSaveResults.edit();
editorSharedPreferencesSaveResults.clear();
editorSharedPreferencesSaveResults
.putString("donePracticeList",jsonSavePracticeResult);
editorSharedPreferencesSaveResults.apply();
}


After that, I retrieved values by the following code:



public ArrayList<KeepPracticeResultsModel> getSavedPracticeResults(){

jsonSavePracticeResult = sharedPreferencesSaveResults.getString("donePracticeList", jsonSavePracticeResult);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<KeepPracticeResultsModel>>() {}.getType();
keepPracticeResultsModels = gson.fromJson(jsonSavePracticeResult, type);
return keepPracticeResultsModels;
}


When app is open, it works fine, but when you try to close app from recent apps, and after that you open app again, SharedPreferences values are lost and jsonSavePracticeResult is Null!
Also I called getSavedPracticeResults() in onResume() and onPause().



I did all of the solutions that existed in StackOverflow but it's happening again!







android sharedpreferences






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 8:42







mhadikz

















asked Dec 28 '18 at 19:50









mhadikzmhadikz

615




615













  • post some more code specially related to sharedPreferencesSaveResults

    – Omer
    Dec 28 '18 at 20:01











  • @Omer I added sharedPreferencesSaveResults in the code.

    – mhadikz
    Dec 29 '18 at 8:44











  • @RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

    – mhadikz
    Dec 29 '18 at 9:51











  • Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

    – Mohammadreza Khatami
    Dec 29 '18 at 18:47



















  • post some more code specially related to sharedPreferencesSaveResults

    – Omer
    Dec 28 '18 at 20:01











  • @Omer I added sharedPreferencesSaveResults in the code.

    – mhadikz
    Dec 29 '18 at 8:44











  • @RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

    – mhadikz
    Dec 29 '18 at 9:51











  • Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

    – Mohammadreza Khatami
    Dec 29 '18 at 18:47

















post some more code specially related to sharedPreferencesSaveResults

– Omer
Dec 28 '18 at 20:01





post some more code specially related to sharedPreferencesSaveResults

– Omer
Dec 28 '18 at 20:01













@Omer I added sharedPreferencesSaveResults in the code.

– mhadikz
Dec 29 '18 at 8:44





@Omer I added sharedPreferencesSaveResults in the code.

– mhadikz
Dec 29 '18 at 8:44













@RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

– mhadikz
Dec 29 '18 at 9:51





@RichardLeMesurier No, because of I debugged app when is running, that method stored all of values and you can retrieve those by return keepPracticeResultsModels, but after closing it from recent apps, if you enter app again jsonSavePracticeResult is null

– mhadikz
Dec 29 '18 at 9:51













Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

– Mohammadreza Khatami
Dec 29 '18 at 18:47





Did you check this ? journaldev.com/9412/android-shared-preferences-example-tutorial I use it before and works like charm! :)

– Mohammadreza Khatami
Dec 29 '18 at 18:47












1 Answer
1






active

oldest

votes


















-1














Have you tried using .commit() instead?



Change



 editorSharedPreferencesSaveResults.apply();


to



 editorSharedPreferencesSaveResults.commit();


The reason being that .apply() writes in the background, asynchronously; wherever as .commit() will write right away. So, if you're closing your app, you want to quickly save these changes before the app is closed.






share|improve this answer
























  • Yes, I tested it actually before sent this post, it didn't work

    – mhadikz
    Dec 29 '18 at 8:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963657%2fsharedpreferences-doesnt-store-values-after-closing-app%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














Have you tried using .commit() instead?



Change



 editorSharedPreferencesSaveResults.apply();


to



 editorSharedPreferencesSaveResults.commit();


The reason being that .apply() writes in the background, asynchronously; wherever as .commit() will write right away. So, if you're closing your app, you want to quickly save these changes before the app is closed.






share|improve this answer
























  • Yes, I tested it actually before sent this post, it didn't work

    – mhadikz
    Dec 29 '18 at 8:22
















-1














Have you tried using .commit() instead?



Change



 editorSharedPreferencesSaveResults.apply();


to



 editorSharedPreferencesSaveResults.commit();


The reason being that .apply() writes in the background, asynchronously; wherever as .commit() will write right away. So, if you're closing your app, you want to quickly save these changes before the app is closed.






share|improve this answer
























  • Yes, I tested it actually before sent this post, it didn't work

    – mhadikz
    Dec 29 '18 at 8:22














-1












-1








-1







Have you tried using .commit() instead?



Change



 editorSharedPreferencesSaveResults.apply();


to



 editorSharedPreferencesSaveResults.commit();


The reason being that .apply() writes in the background, asynchronously; wherever as .commit() will write right away. So, if you're closing your app, you want to quickly save these changes before the app is closed.






share|improve this answer













Have you tried using .commit() instead?



Change



 editorSharedPreferencesSaveResults.apply();


to



 editorSharedPreferencesSaveResults.commit();


The reason being that .apply() writes in the background, asynchronously; wherever as .commit() will write right away. So, if you're closing your app, you want to quickly save these changes before the app is closed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 20:02









Advice-DogAdvice-Dog

1,54421231




1,54421231













  • Yes, I tested it actually before sent this post, it didn't work

    – mhadikz
    Dec 29 '18 at 8:22



















  • Yes, I tested it actually before sent this post, it didn't work

    – mhadikz
    Dec 29 '18 at 8:22

















Yes, I tested it actually before sent this post, it didn't work

– mhadikz
Dec 29 '18 at 8:22





Yes, I tested it actually before sent this post, it didn't work

– mhadikz
Dec 29 '18 at 8:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963657%2fsharedpreferences-doesnt-store-values-after-closing-app%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas