0

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 thisenter image description here when the user click the button the it update the setting activity enter image description here but when i press the back button from the fragment the background is not getting updated nither the sharedpreference value.

SAVVY
  • 842
  • 16
  • 33
  • share the code you are using for storing data in sharedpreferences – Umar Ata Jul 03 '17 at 08:53
  • I assume `"YOUR_PREF_NAME"` is a string constant and should not be quoted – OneCricketeer Jul 03 '17 at 08:53
  • no problem with this "YOUR_PREF_NAME" it will work properly, getSharedPreferences method just require a string name to make preference file – Umar Ata Jul 03 '17 at 08:54
  • i added the code how i am storing the shared preference – SAVVY Jul 03 '17 at 08:57
  • Regardless of what is being attempted in the question, and no clear error. It's a duplicate. https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – OneCricketeer Jul 03 '17 at 08:57
  • @SAVVY what is `position`? Are you in some adapter there? SharedPreferences can't assign duplicate keys – OneCricketeer Jul 03 '17 at 08:59
  • @cricket_007 i am able to fetch the data but the thing is it is not getting updated when i press the back button from the them fragment – SAVVY Jul 03 '17 at 08:59
  • position is the image position of the image that the user want to set ans background – SAVVY Jul 03 '17 at 09:00
  • i am showing the images in a view pager – SAVVY Jul 03 '17 at 09:01
  • Your question is missing a ViewPager, so it's hard to understand. I doubt that the problem is the SharedPreferences, but how you're expecting it to work. Please show a [mcve] – OneCricketeer Jul 03 '17 at 09:03
  • @cricket_007 sir I added all the things now .Hope you can help me – SAVVY Jul 03 '17 at 09:14
  • Okay, now, what is the error you experience? Does it only load the first item, or the one you click? Is your image array the exact same in the Activity as the adapter? – OneCricketeer Jul 03 '17 at 09:18
  • ya it is same ,the error is it does not load the one i clicked – SAVVY Jul 03 '17 at 09:34

1 Answers1

0

Use editor.commit() to save immediately. commit() writes the data synchronously where as apply() schedules the data to be written asynchronously

Praveen
  • 697
  • 6
  • 21
  • I tried that bro and i tried to put this in every activity lifecycle method then only i am asking in stack – SAVVY Jul 03 '17 at 09:11
  • from where your are opening the setting activity (the second screen you have shared)? It is not from view pager item click listener – Praveen Jul 03 '17 at 09:28
  • no sir the view pager is just to set the shared preference ,i am opening the setting activity from the main activity or pressing back button from the them fragment – SAVVY Jul 03 '17 at 09:38
  • are you getting zero th position every time ? Have you debug the code and check whether view pager on click is getting called? – Praveen Jul 03 '17 at 09:43
  • ya it is getting called but it is not getting updated when i press the back button from the theme fragment – SAVVY Jul 03 '17 at 09:47