I'm developing an android application where a number of String
, String[]
, int
and int[]
are used. I have to pass this variable from one activity to another using intent.putExtra()
. But it seems to me a little bit disgusting.I love to optimize my code.
I would use these variable as static
. But using too much static
data can cause memory leak.
So what is the right way to manage randomly used variable among different activity of android?
Thanks in advance.

- 3,357
- 3
- 27
- 34

- 63
- 2
- 10
-
Use SharedPreferences for storing retrieving value – Subhalaxmi Apr 30 '14 at 06:30
-
use Singleton http://stackoverflow.com/a/7886046/1012284 – Padma Kumar Apr 30 '14 at 06:58
-
I have implemented the Singleton model. It is dynamic and light coded to implement. Thank you Sir ! – Tushar Saha Apr 30 '14 at 13:07
3 Answers
Indeed, static variables cause memory leak and NullPointerExceptions
.
You can save strings and String arrays to SharedPreferences
so here is a solution for you (always remember to do a null check for the data you want to get from SharedPreferences):
SharedPreferences class for setting and getting your objects:
public class SharedPrefManager {
public static boolean saveArray(String[] array, String arrayName, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(arrayName +"_size", array.length);
for(int i=0;i<array.length;i++)
editor.putString(arrayName + "_" + i, array[i]);
return editor.commit();
}
public static String[] loadArray(String arrayName, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
int size = prefs.getInt(arrayName + "_size", 0);
String array[] = new String[size];
for(int i=0;i<size;i++)
array[i] = prefs.getString(arrayName + "_" + i, null);
return array;
}
public static void setDefaults(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
Setting array to SharedPreferences: clear and update your array into sharedpreferences.
//save array to shared pref
SharedPrefManager.saveArray(array, "array_tag", getApplicationContext());
Getting array from SharedPreferences:
String [] a = SharedPrefManager.loadArray("array_tag", getApplicationContext());
Save String to SharedPreferences
SharedPrefManager.setDefaults("your_tag", "string_value", context);
Get String from SharedPreferences
SharedPrefManager.getDefaults("your_tag", getApplicationContext())

- 3,965
- 2
- 22
- 39
In my opinion for handling and using large amount of data you should go for the Array List of string.
About the another problem you are facing is to carry all data. You can easily pass an array list from one activity to another activity.
Check out this simple example for it :
Suppose "CurrentActivity.java" is an activity from where you are suppose to send array list
At the first you should have to declare and initialise array list like:
ArrayList<String> listOfItems;
listOfItems = new ArrayList<String>();
Put some values in the array list :
listOfItems.add("Add all of your items here..");
Now in the CurrentActivity put this code on any button click :
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putStringArrayListExtra("itemList", listOfItems);
And "NewActivity.java" will be an activity which will get the array list from CurrentActivity
So in the New Activity selectedList will be another arraylist object, now you just have to put :
Bundle i1 = getIntent().getExtras();
selectedList = i1.getStringArrayList("itemList");
And its done. Hope it will help you. :)

- 1,713
- 1
- 11
- 19
use shared preference for this,once you stored the data you can access the value in entire application.
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("EMP_ID",mUname);
editor.commit(); //commit is important for storing the value
using below code you can retrieve the value in any activity
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
final String myVariable = prefs.getString("EMP_ID", "mUname");

- 3,092
- 2
- 21
- 33