Global variable data getting lost Android

Asked

Viewed 397 times

0

Hello to all
I’m creating an app that when the person logs in, the app brings the data Nome, Login, Senha and Foto(a url) of the server database, and saves that data in a Singleton so that they are used in all other activitys. So far so good, everything working fine, but when I made a button for the person to choose a photo from the gallery and upload it (all this is working), I realized that if I take a while to choose the photo and stay waiting in the gallery, the data I recorded on Singleton 'get lost' (as if they were erased or whatever).
I was wondering if there is a way to record this data without them 'getting lost' when I leave my application in background.
Note: I did not post any code because I believe it is not necessary, since there is no error in it, but the data that 'disappear', I believe that android is somehow erasing them to free memory or something. But if you need, just ask me to edit the question and put the code.

  • 1

    Why you don’t use Sharedpreferences to store this information?

1 answer

4


Suggestion to use instead of Singleton use the class SharedPreferences android.

Example of use:

1. Name of your preferences

public static final String PREFS_NAME = "YourPreferences";

2. Restores the preferences

The second parameter of the method [getShardePreferenses](http://developer.android.com/intl/pt-br/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int)) is the writing mode: constants for use MODE_PRIVATE, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE

SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

3. Restores a new instance of SharedPreferences.Editor to create/modify the values in the object SharedPreferences

SharedPreferences.Editor editor = settings.edit();

4. Save a new preface

editor.putString("Nome", "valorAqui");

5. Confirms the data recording

editor.commit();

6. To recover the data just repeat the steps 1 and 2 and the second line:

settings.getString("Nome", "");

How can you store Strings can also store Integer, Long, Float, Double and Boolean.

Useful link

  • 1

    Congratulations on the explanation Friend! Surigo just explain that in step 2 , the second parameter is the writing mode, and recommend the use of Constants ( Context.MODE_PRIVATE, Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE )

  • @Thiagoluizdomacoski Thanks friend for the tip. Update in reply!

  • Thanks for the answer I’ll try to implement in my code.

  • Apologizing, but I forgot to mention that the class that returns the data from the json database user is running in a separate Thread (Asynctask), and I don’t think that’s why I’m able to use getSharedPreferences(). I am trying to use Sharedpreferences as an equal class in the link that Voce passed.

  • @Bruno you’re trying to give the getSharedPreferences within the AsyncTask?

  • No, I’ll explain the sequence. I have Mainactivtity with the fields for user and password, when clicking the button to log in, the login and password data are sent to the Loginbackground class (which extends Asynctask) which sends this data (in the Ackground method) to the Loginhttp class, which sends this data to a php webservice and returns the data of that user in json, then in that same class(Loginhttp) I saved(I try to save) the data in Sharedpreferences(I created a Shareduserpreferences class equal in the link you passed)but I can’t use getSharedPreferences().

  • @Bruno You can improve your class SharedUserPreferences putting a property on it like Context and in her builder receive this Context and do the proper attribution of the property. Hence in it just use so: meuContext.getSharedPreferences("",Context.MODE_PRIVATE);

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.