我正在开发一个Android应用程序,其中使用了许多String
,String[]
,int
和int[]
。我必须使用intent.putExtra()
将此变量从一个活动传递到另一个活动。但在我看来有点恶心。我喜欢优化我的代码。
我会将这些变量用作static
。但是使用过多的static
数据会导致内存泄漏。
那么在android的不同活动中管理随机使用变量的正确方法是什么?
提前谢谢。
答案 0 :(得分:1)
实际上,静态变量会导致内存泄漏和NullPointerExceptions
。
您可以将字符串和字符串数组保存到SharedPreferences
,因此这里有一个解决方案(始终记得对要从SharedPreferences获取的数据进行空检查):
用于设置和获取对象的SharedPreferences类:
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);
}
}
将数组设置为SharedPreferences: 清除并将您的数组更新为共享首选项。
//save array to shared pref
SharedPrefManager.saveArray(array, "array_tag", getApplicationContext());
从SharedPreferences获取数组:
String [] a = SharedPrefManager.loadArray("array_tag", getApplicationContext());
将字符串保存到SharedPreferences
SharedPrefManager.setDefaults("your_tag", "string_value", context);
从SharedPreferences获取字符串
SharedPrefManager.getDefaults("your_tag", getApplicationContext())
答案 1 :(得分:1)
在我看来,处理和使用大量数据你应该去 数组字符串列表。
关于您面临的另一个问题是携带所有数据。您可以轻松地将数组列表从一个活动传递到另一个活动。
查看这个简单的例子:
假设&#34; CurrentActivity.java&#34;是您要发送数组列表的活动
首先,您必须声明并初始化数组列表,如:
ArrayList<String> listOfItems;
listOfItems = new ArrayList<String>();
在数组列表中放入一些值:
listOfItems.add("Add all of your items here..");
现在,在CurrentActivity中,将此代码放在任何按钮上:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putStringArrayListExtra("itemList", listOfItems);
和&#34; NewActivity.java&#34;将是一个活动,它将从CurrentActivity
获取数组列表所以在New Activity中 selectedList将是另一个arraylist对象,现在你只需要放:
Bundle i1 = getIntent().getExtras();
selectedList = i1.getStringArrayList("itemList");
完成了。希望它会对你有所帮助。 :)
答案 2 :(得分:0)
使用共享首选项,一旦存储了数据,就可以访问整个应用程序中的值。
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
使用以下代码,您可以检索任何活动中的值
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
final String myVariable = prefs.getString("EMP_ID", "mUname");