How to create a function in which you would pass the settings. For example, we have a MainActivity in it there is a fragment. There is a button by which we go to the dialog fragment of the settings where we fill in some information (how old, birth and the like). How to write a function that would remember all the filled fields and the next time we press this button again, the same settings would remain.

PS There is Activation, Settings fragment (button in the toolbar) and Fragment (always shown in Activiti)

    1 answer 1

    Use SharedPreferences

    We write down:

    String name = ...; SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); pref.edit().putString("name", name).apply(); 

    Read:

     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); String name = pref.getString("name", ""); 

    Those. at the time of recording, you save your data, and when you show a fragment, you can get it and display it

    • But where does he save them? - Satanist Devilov
    • @SatanistDevilov, in the internal folder of the application, in the sharedpreferences.xml xml file - Yuriy SPb
    • it turns out "name" - a label, and name - what should be written down? - Satanist Devilov
    • and why the second argument is empty? pref.getString("name", ""); - Satanist Devilov
    • one
      @SatanistDevilov, info stored by key-value pairs. The first argument in both cases is the key. The second - in the first case, the value that you want to write down, i.e. username, for example, taken from the input field. In the second case, the second argument, the empty string, is the default value that will be returned if there is no key-value pair in the file —YuriSPb