How to make android app data, app-wide available?










-1















How can I make data available through my whole android app?
For instance, I want to display a user picture in the toolbar in every screen. Also, the name of the user is displayed in the navigation drawer in every screen.



Is a singleton the right approach for this? Or are other techniques better?



I have been googling a lot, but i can't find a good approach for my use case so far.










share|improve this question
























  • save those values to shared preferences, and retrieve whenever you need.

    – Karan Mer
    Nov 13 '18 at 11:03











  • Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

    – Fantômas
    Nov 13 '18 at 11:09
















-1















How can I make data available through my whole android app?
For instance, I want to display a user picture in the toolbar in every screen. Also, the name of the user is displayed in the navigation drawer in every screen.



Is a singleton the right approach for this? Or are other techniques better?



I have been googling a lot, but i can't find a good approach for my use case so far.










share|improve this question
























  • save those values to shared preferences, and retrieve whenever you need.

    – Karan Mer
    Nov 13 '18 at 11:03











  • Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

    – Fantômas
    Nov 13 '18 at 11:09














-1












-1








-1








How can I make data available through my whole android app?
For instance, I want to display a user picture in the toolbar in every screen. Also, the name of the user is displayed in the navigation drawer in every screen.



Is a singleton the right approach for this? Or are other techniques better?



I have been googling a lot, but i can't find a good approach for my use case so far.










share|improve this question
















How can I make data available through my whole android app?
For instance, I want to display a user picture in the toolbar in every screen. Also, the name of the user is displayed in the navigation drawer in every screen.



Is a singleton the right approach for this? Or are other techniques better?



I have been googling a lot, but i can't find a good approach for my use case so far.







android user-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:09







Peter van Leeuwen

















asked Nov 13 '18 at 11:01









Peter van LeeuwenPeter van Leeuwen

816




816












  • save those values to shared preferences, and retrieve whenever you need.

    – Karan Mer
    Nov 13 '18 at 11:03











  • Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

    – Fantômas
    Nov 13 '18 at 11:09


















  • save those values to shared preferences, and retrieve whenever you need.

    – Karan Mer
    Nov 13 '18 at 11:03











  • Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

    – Fantômas
    Nov 13 '18 at 11:09

















save those values to shared preferences, and retrieve whenever you need.

– Karan Mer
Nov 13 '18 at 11:03





save those values to shared preferences, and retrieve whenever you need.

– Karan Mer
Nov 13 '18 at 11:03













Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

– Fantômas
Nov 13 '18 at 11:09






Retrieve the data everywhere you need it. Create a connection, consume the data, destroy the connection.

– Fantômas
Nov 13 '18 at 11:09













1 Answer
1






active

oldest

votes


















2














To use shared preferences:



SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();


to save in shared preferences:



editor.putString("tag", "save me please");
editor.apply();


to retrieve from shared preferences:



String s = preferences.getString("tag", "default_value");


Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here






share|improve this answer























  • Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

    – Peter van Leeuwen
    Nov 15 '18 at 20:02











  • yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

    – Nikos Hidalgo
    Nov 15 '18 at 20:05











  • oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

    – Peter van Leeuwen
    Nov 15 '18 at 20:07











  • have a look at this: stackoverflow.com/a/29625876/10300673

    – Nikos Hidalgo
    Nov 15 '18 at 20:10











  • after you download it, you use the local path in the shared preferences as described above

    – Nikos Hidalgo
    Nov 15 '18 at 20:10










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%2f53279571%2fhow-to-make-android-app-data-app-wide-available%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









2














To use shared preferences:



SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();


to save in shared preferences:



editor.putString("tag", "save me please");
editor.apply();


to retrieve from shared preferences:



String s = preferences.getString("tag", "default_value");


Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here






share|improve this answer























  • Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

    – Peter van Leeuwen
    Nov 15 '18 at 20:02











  • yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

    – Nikos Hidalgo
    Nov 15 '18 at 20:05











  • oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

    – Peter van Leeuwen
    Nov 15 '18 at 20:07











  • have a look at this: stackoverflow.com/a/29625876/10300673

    – Nikos Hidalgo
    Nov 15 '18 at 20:10











  • after you download it, you use the local path in the shared preferences as described above

    – Nikos Hidalgo
    Nov 15 '18 at 20:10















2














To use shared preferences:



SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();


to save in shared preferences:



editor.putString("tag", "save me please");
editor.apply();


to retrieve from shared preferences:



String s = preferences.getString("tag", "default_value");


Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here






share|improve this answer























  • Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

    – Peter van Leeuwen
    Nov 15 '18 at 20:02











  • yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

    – Nikos Hidalgo
    Nov 15 '18 at 20:05











  • oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

    – Peter van Leeuwen
    Nov 15 '18 at 20:07











  • have a look at this: stackoverflow.com/a/29625876/10300673

    – Nikos Hidalgo
    Nov 15 '18 at 20:10











  • after you download it, you use the local path in the shared preferences as described above

    – Nikos Hidalgo
    Nov 15 '18 at 20:10













2












2








2







To use shared preferences:



SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();


to save in shared preferences:



editor.putString("tag", "save me please");
editor.apply();


to retrieve from shared preferences:



String s = preferences.getString("tag", "default_value");


Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here






share|improve this answer













To use shared preferences:



SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();


to save in shared preferences:



editor.putString("tag", "save me please");
editor.apply();


to retrieve from shared preferences:



String s = preferences.getString("tag", "default_value");


Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 11:28









Nikos HidalgoNikos Hidalgo

893115




893115












  • Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

    – Peter van Leeuwen
    Nov 15 '18 at 20:02











  • yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

    – Nikos Hidalgo
    Nov 15 '18 at 20:05











  • oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

    – Peter van Leeuwen
    Nov 15 '18 at 20:07











  • have a look at this: stackoverflow.com/a/29625876/10300673

    – Nikos Hidalgo
    Nov 15 '18 at 20:10











  • after you download it, you use the local path in the shared preferences as described above

    – Nikos Hidalgo
    Nov 15 '18 at 20:10

















  • Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

    – Peter van Leeuwen
    Nov 15 '18 at 20:02











  • yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

    – Nikos Hidalgo
    Nov 15 '18 at 20:05











  • oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

    – Peter van Leeuwen
    Nov 15 '18 at 20:07











  • have a look at this: stackoverflow.com/a/29625876/10300673

    – Nikos Hidalgo
    Nov 15 '18 at 20:10











  • after you download it, you use the local path in the shared preferences as described above

    – Nikos Hidalgo
    Nov 15 '18 at 20:10
















Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

– Peter van Leeuwen
Nov 15 '18 at 20:02





Does this also apply for the use case I described above? I need to display images in the toolbar and navigation drawer and the name of the user. All those widgets are there in every screen of the app.

– Peter van Leeuwen
Nov 15 '18 at 20:02













yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

– Nikos Hidalgo
Nov 15 '18 at 20:05





yes, you can use putString for the user's name, and the path of the user's photo. Then have a method that will read the strings from the shared preferences and populate your views at the toolbar/navigation drawer

– Nikos Hidalgo
Nov 15 '18 at 20:05













oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

– Peter van Leeuwen
Nov 15 '18 at 20:07





oke, but the image is retrieved from an online location. This means it needs to be downloaded every time the screen is loaded right? So how I can download and store it once and retrieve it from that location every next time?

– Peter van Leeuwen
Nov 15 '18 at 20:07













have a look at this: stackoverflow.com/a/29625876/10300673

– Nikos Hidalgo
Nov 15 '18 at 20:10





have a look at this: stackoverflow.com/a/29625876/10300673

– Nikos Hidalgo
Nov 15 '18 at 20:10













after you download it, you use the local path in the shared preferences as described above

– Nikos Hidalgo
Nov 15 '18 at 20:10





after you download it, you use the local path in the shared preferences as described above

– Nikos Hidalgo
Nov 15 '18 at 20:10

















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%2f53279571%2fhow-to-make-android-app-data-app-wide-available%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党