Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a shared preferences file first created?

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?

like image 349
Smitha Avatar asked Mar 05 '12 06:03

Smitha


People also ask

How is shared preference implemented?

You can create a new shared preference file or access an existing one by calling one of these methods: getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.

Where shared preferences are stored?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

How is data stored in shared preferences?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

Where shared preferences are stored in Windows?

SharedPreferences are stored in an xml file in the app data folder, i.e. SharedPreferences added during runtime are not stored in the Eclipse project. The default shared preferences file would actually be: /data/data/<package>/shared_prefs/<package>_preferences. xml .


2 Answers

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 - SharedPreferences should be the only way to access this file.

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

like image 66
a.ch. Avatar answered Nov 15 '22 10:11

a.ch.


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

You don't need to create any files when dealing with PreferenceScreens or SharedPreferences. That is handled by SharedPreferences behind the scenes. I believe that happens when you first set a value in the Preference screen but I honestly am not sure.

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

That code would get the SharedPreferences for your PreferenceScreen once they have been set. If they haven't been set, you use the default values.

like image 29
Knossos Avatar answered Nov 15 '22 09:11

Knossos