When is the general preference file created first?

I wanted to know when the first preference file is created the first time?

I have the following code:

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="User settings"> <EditTextPreference android:title="User Name" android:key="userName" android:summary="Please Enter User Name"></EditTextPreference> <EditTextPreference android:title="Password" android:key="password" android:summary="Password Here" android:inputType="textPassword"></EditTextPreference> </PreferenceCategory> </PreferenceScreen> public class PrefsActivity extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); } } 

Also, how can I use getSharedpreference () with a file name? I do not know if I should first create this file and put it in the application data directory?

I meant when the general preferences file is created first: when is the application installed or after a while? If later, when?

+6
source share
5 answers

The getSharedPreferences(name, mode) method automatically creates a file with the specified name, so you do not need to create it. In fact, the exact location and name of this preference file is not documented, so I suggest you not rely on some conventions when trying to access this file directly , since the location and name may be changed in the future - SharedPreferences should be the only way to access this file.

A preference file with a specific name is created when getSharedPreferences(name, mode) or addPreferencesFromResource(preferencesResId) is called for the first time.

+12
source

I am not 100% sure what you are talking about here.

You do not need to create files when working with PreferenceScreens or SharedPreferences. This is handled by SharedPreferences behind the scenes. I believe this happens when you first set a value on the Preferences screen, but I'm honestly not sure.

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); String username = preferences.getString("username", "defaultvalue"); String password = preferences.getString("password", "defaultvalue"); 

This code will receive SharedPreferences for your PreferenceScreen after they are installed. If they were not installed, you use the default values.

+2
source

General settings will be created in / data / data / appname / shared_prefs / prefs_name.xml

0
source

As mentioned in other comments, you will never interact directly with general settings files. You read and write values ​​for them using the SharedPreferences object. The Context.getSharedPreferences() method takes as its first argument a string (the name of the shared preferences file you want to interact with) and returns a SharedPreferences object. According to the documentation for the getSharedPreferences() method:

If a preference file by this name does not exist, it will be created when you extract the editor (SharedPreferences.edit ()) and then commit the changes (Editor.commit ()).

Thus, a general settings file is created. Other methods that return SharedPreferences objects simply do the same thing in different contexts.

For instance:

Activity.getPreferences() method

just calls the base getSharedPreferences (String, int) method, passing that activity class name as the preference name.

0
source

You must set the activity preference intent in onCreateOptionMenu .

 public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.setting: startActivity(new Intent(this,Settingpage.class)); Toast.makeText(this, "setting clicked", Toast.LENGTH_LONG).show(); break; } return true; } 
-4
source

Source: https://habr.com/ru/post/909972/


All Articles