Update the fragment after changes in annex settings



  • There's a driveway with fragments and one activism, and this activity is all built up. Starting the app, launching the fragment, then calling the roadblock and selecting the building activity, selecting the parameters we need and pushing the button back, reverting to the fragment from which we caused the building activity and the fragmentation, if we choose another fragment through the diver, and then re changes will be visible. The question is, how do we update the fragment after reversing it from activity?



  • To this end, we need to notify the fragment of the changes in construction and apply them.

    1. Indicate the fragment to implement the necessary interface:

      implements SharedPreferences.OnSharedPreferenceChangeListener
      
    2. Record in the fragment the alternator. Add it. onCreate fragment:

      PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
      
    3. An interface is being implemented, which is defined as the alteration on its key and some of the altered designs:

      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
      {
          Log.i("WOW!", "onSharedPreferenceChanged with key: " + key);
          if (!isAdded())
          {
              return;
          }
          if (key.equals(ТУТ_К_Л_КЛЮЧ_ИЗ_НАСТРОЕК))
          {
             //Вот в этом месте вы можете менять что-то во фрагменте в момент
             //изменения конкретной настройки
             //Если лень менять всё руками можно поступить плохо и просто пересоздать активити: getActivity().recreate();
          }
      }
      

    Another option is to apply the designs at the time the fragment is displayed on the screen, i.e. in the method onResume()

    @Override
    public void onResume()
    {
        super.onResume();
        //назначаем, например, цвет текста и настроек
        int btnFontColor = prefs.getInt("key_pref_custom_inter_color_font_button", getResources().getColor(R.color.color_inter_color_font_button));
        btnKnow.setTextColor(btnFontColor);
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2