I wanted to know when is a shared preference file created for 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 filename? I don’t know if I have to first create this file and place it in app’s data directory?
I meant When is a shared preferences file first created: when the application is installed, or some time later? If later, when?
The
getSharedPreferences(name, mode)method automatically creates the file with the name specified, so you don’t need to create it. Actually, the exact location and name of this preference file is not documented, so I’d suggest you don’t rely on some conventions when trying to access this file directly, since the location and name may be changed in future –SharedPreferencesshould be the only way to access this file.The preference file with certain name is created when
getSharedPreferences(name, mode)oraddPreferencesFromResource(preferencesResId)is called for the first time.