how to use the SharedPreferrence value in another activity in android app

℡╲_俬逩灬. 提交于 2019-12-12 05:51:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!