简体   繁体   中英

Reusing PreferenceFragment with multiple preference files

Scenario: My android application is acting as a server. There are N number of clients that are connected to the server. There is a set of settings for the clients which user can configure from the server (android app). so basically I am sending the user preferences over the socket to respective client. The set of settings is same for all the clients but I need to maintain the the settings state for each client. for example:

Set of Settings:

Settings 1
Settings 2
Settings 3

Now user configures the above set for 2 clients as follows: For client1:

Settings 1 :true
Settings 2 :false
Settings 3 :false

For Client2:

Settings 1 :false
Settings 2 :false
Settings 3 :false

now in the android app when user selects the client1 then client1 specific set should be shown.

Problem: I am not able to save client wise state of settings.

My Expertise: I am a beginner in android programming.

My progress so far: As per my understanding of the scenario I need to make client wise different-2 shared preference file. I created a generic preference fragment for the set of settings. As the settings is same for all the clients so I am reusing the preference fragment. I also tried to specify preference files dynamically for each client:

       SharedPreferences sharedPreference = context.getSharedPreferences(
                "<Here I dynamically specify prefs file name>", Context.MODE_PRIVATE);

but it is not working.

Please help.

Answering to my own question, hope this may help someone in future: We need to create different-2 preference files in this scenario. we can do this as follows:

getPreferenceManager().setSharedPreferencesName("preference_file_name");

and later on to load that particular preference file

getSharedPreferences("preference_file_name", Context.MODE_PRIVATE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM