I am trying to set the shared preference for the theme of the app MainActivity-> Settings Activity -> Theme fragment and in the theme fragment .I am displaying the images for the background when clicked it will set the position and the position will be received by the activity to set the image view but the thing is when i click on the fragment image it is taking the new value but in the setting fragment i am not able to get the updated value . But in the main activity it is getting updated but when i come back from the main activity to the setting activity it is getting updated .This is the code that i am trying
@Override
protected void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0);
Glide.with(Settings.this).load(img[snowDensity])
.thumbnail(1f)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
}
this way i am setting the shared preference
private class MyPagerAdapter extends PagerAdapter {
private int img[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6
};
@Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView view = new ImageView(getActivity());
Glide.with(getActivity()).load(img[position])
.thumbnail(1f)
.fitCenter()
.error(R.drawable.music)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity().getApplicationContext(), "Ringtone Set" + position, Toast.LENGTH_SHORT).show();
SharedPreferences settings = getActivity().getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY", position);
editor.commit();
}
});
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return 6;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
}
is there any other way to update data.
How i am expecting it to work .I am showing in a view pager like this when the user click the button the it update the setting activity
but when i press the back button from the fragment the background is not getting updated nither the sharedpreference value.