Reputation: 53
I always get default in my shared preference , why is this happening? Here is the part where I insert value:
holder.camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RowData rd = getItem(position); //get list_row from i
System.out.println("OnClick Camera");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SharedPreferences prefs = (mContext).getSharedPreferences(
"com.oxtro.trustea", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("crit_id_pref",String.valueOf(rd.criteria_id));
prefEditor.commit();
((Activity)mContext).startActivityForResult(takePicture, 0);
}
});
Here is where I retrieve its value, the value is always fetched as default rather than the needed one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
Uri uri = data.getData();
System.out.println("File path is " + uri.toString());
String path = getRealPathFromURI(uri);
System.out.println("Real path is " + path);
imageupload= new ImageUploadManager(ChapterActivity.this);
imageupload.open();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ChapterActivity.this);
String t_critid = preferences.getString("crit_id_pref", "DEFAULT");
System.out.println("@OnActivityResult | shared pref crit id: "+t_critid);
}
Upvotes: 0
Views: 528
Reputation: 13483
In onActivityResult
,
Change this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ChapterActivity.this);
To this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences("com.oxtro.trustea", Context.MODE_PRIVATE);
You need to keep the Preference your referencing consistent. When you write to a Preference with one name, you need to read from it with the same name as well.
Upvotes: 1
Reputation: 4574
Make this call in your second activity too, to get the result:
SharedPreferences prefs = (mContext).getSharedPreferences(
"com.oxtro.trustea", Context.MODE_PRIVATE);
Upvotes: 1