问题
in my app i am storing a value in Shared Preference and it gets stored successfully. Following is a piece of my code
SharedPreferences prefs = getSharedPreferences( "idValue", 0 );
Editor editor = prefs.edit();
editor.putString( "idValue", chap.getid() );
editor.commit();
Log.e("Shared Pref",""+chap.getid());
Now i want to use this value in some other activities with a condition whether it is null or has an value.
How to get this value in another activity.....
回答1:
You can simply use
SharedPreferences prefs = this.getSharedPreferences("idValue", MODE_WORLD_WRITEABLE);
String idvalue = prefs.getString("idValue", "");
in any activity you need
回答2:
first define Your public static SharedPreferences prefs = getSharedPreferences( "idValue", 0 );
as static in your main activity.
and other activity you can get value like this.
firstactivity_name.prefs.getString("idValue", null);
来源:https://stackoverflow.com/questions/6234902/how-to-use-the-sharedpreferrence-value-in-another-activity-in-android-app