如何在Android应用程序的另一个活动中使用SharedPreferrence值

问题描述

|| 在我的应用程序中,我在“共享首选项”中存储了一个值,并且成功存储了该值。 以下是我的一段代码
SharedPreferences prefs = getSharedPreferences( \"idValue\",0 );
Editor editor = prefs.edit();
editor.putString( \"idValue\",chap.getid() );
editor.commit();
Log.e(\"Shared Pref\",\"\"+chap.getid());
现在我想在某些其他活动中使用此值,条件是它为null还是具有值。 如何在另一个活动中获取此值.....     

解决方法

        您可以简单地使用
SharedPreferences prefs = this.getSharedPreferences(\"idValue\",MODE_WORLD_WRITEABLE);
String idvalue = prefs.getString(\"idValue\",\"\");
在您需要的任何活动中     ,        首先在您的主要活动中将“ѭ2”定义为静态。 和其他活动,您可以像这样获得价值。
firstactivity_name.prefs.getString(\"idValue\",null);