I have several activations, to which when I click on the menu, a SearchBar appears and the user can move the slider to change the text size. In each of the ativiti implemented such code

private static SharedPreferences sharedPref; public static final String FONT_SIZE = "FONT_SIZE"; @Override protected void onCreate(Bundle savedInstanceState) { final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(this); loadSizeFont(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { sizeFont = progress; tv_text.setTextSize(sizeFont); saveSizeFont(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } void saveSizeFont() { sharedPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sharedPref.edit(); ed.putInt(FONT_SIZE, sizeFont); ed.apply(); } void loadSizeFont() { sharedPref = getPreferences(MODE_PRIVATE); int size = (int) sharedPref.getInt(FONT_SIZE, 17); tv_text.setTextSize(size); seekBar.setProgress(size); } 

If in activation 1, the user has increased the font, then switched to activation 2, there you need to call seekbar again and move the slider. This is not very convenient. It is necessary that by changing the size of the size in one place, it would change in the rest of the necessary activations. Tell me how to implement it correctly?

  • 2
    Read saved SharedPreferences in each activity. - Enikeyschik

1 answer 1

You can either store the data in SharedPreferences and read them out if necessary, as you have already been advised.

You can make such a singleton:

 public class DataSaver { private static DataSaver dataSaver; private static final String PREFS = "prefs"; private static final String T_SIZE = "size"; private int textSize; public DataSaver get (Context ctx) { if (dataSaver ==null) dataSaver= new DataSaver(ctx); return dataSaver; } private DataSaver(Context ctx) { textSize = ctx.getSharedPreferences(PREFS,Context.MODE_PRIVATE).getInt(T_SIZE,17); } public int getTextSize() { return textSize; } public void setTextSize(Context ctx, int textSize) { ctx.getSharedPreferences(PREFS,Context.MODE_PRIVATE).edit().putInt(T_SIZE,textSize).apply(); this.textSize = textSize; } } 

In the right place / activity call: DataSaver.get(MyActivity.this).getTextSize();

Change value: DataSaver.get(MyActivity.this).setTextSize(MyActivity.this, textSize);

It can be used to store any data inside the application, except, perhaps, Context - in the latter case a memory leak is possible.

You can make a custom TextView :

 public class MyTV extends TextView { public static final String PREFS = "prefs"; public static final String T_SIZE = "size"; public MyTV(Context context) { super(context); init(context); } private void init(Context context) { setTextSize(context.getSharedPreferences(PREFS,Context.MODE_PRIVATE).getInt(T_SIZE,17)); } public MyTV(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } public MyTV(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public MyTV(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context); } } 

install it wherever it is necessary (only, in the place where you will overwrite the size, it will need to be redrawn).