title: Shared preferences value not updating the second time android
body: <p>I have used a Broadcast receiver to  change the value of a variable inside my fragment every 24 hours.</p>
  
  <p>Since the value of the variable gets reinitialized to previous initialization when the fragment restarts I have used shared preferences to save the value every time so that it does not reinitialize again and again.</p>
  
  <p>The problem is that the value is changed once and is not updating again. so if the value is 10 it changes to 11 but then does not go to 12.</p>
  
  <p>This is the broadcast receiver</p>
  
  <pre><code>public class AlarmReciever extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
  
      String intentImageName = intent.getStringExtra("imageName");
      int numberImageName = Integer.parseInt(intentImageName) +1;
      EventBus.getDefault().post(new ImageNameEvent(""+numberImageName));;
  
  }
  </code></pre>
  
  <p>This is the EventBus function used in the fragment to get the value from the BroadcastReceiver </p>
  
  <pre><code>  @Subscribe
  public void onEvent(ImageNameEvent event) {
      imagename = Integer.parseInt(event.getMessage());
      SharedPreferences.Editor editor = preferences.edit();
      editor.putInt("image", imagename);
      editor.apply();
  }
  </code></pre>
  
  <p>This is the onCreate function  of the Fragment where the value of the shared preferences is retrieved.</p>
  
  <pre><code>    @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      scheduleAlarm();
  
      preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
      int name = preferences.getInt("image", 0);
      if (name != 0) {
          imagename = name;
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>