I am trying to create a list of certaing objects, so i can see it anytime and anywhere. So i was wondering if there is any way to have some information in an Android activity, and sees it in the other activities just like the Session[] in asp.net.
Asked
Active
Viewed 791 times
4 Answers
3
You can use Android Shared Preferences
SharedPreferences prefs = getSharedPreferences("myPreferences",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("email", "test@email.com");
editor.putString("name", "Test");
editor.commit();
And to get the data use this:
SharedPreferences prefs =
getSharedPreferences("myPreferences",Context.MODE_PRIVATE);
String email= prefs.getString("email", "default_email@email.com");

mintypa
- 46
- 2
-
But, in the case I wanna use a list of objects creates for me, instead of "putString", how would it be, with serialize? .... – user1568613 Aug 22 '12 at 16:02
-
If you want to store an object, you could transform it to an JsonObject with Gson, but remember that you shouldnt store to much information here. See this example: http://androidcodemonkey.blogspot.com.es/2011/07/store-and-get-object-in-android-shared.html – mintypa Aug 22 '12 at 16:26
2
Check out the Android Application
class. It's essentially a singleton with the lifetime of your app.
Or as others have suggested, use Shared Preferences to a) persist things across Activities
and b) persist things across "sessions".

Jon O
- 6,532
- 1
- 46
- 57
1
This or simple Shared Preferences might be what you are looking for.

Community
- 1
- 1

Kazekage Gaara
- 14,972
- 14
- 61
- 108
0
You can push all your shared information in a static object.
The best way to do it is to create a singleton. Here is a sample: http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

Aerilys
- 1,628
- 1
- 16
- 22