How to iterate through all keys of shared preferences?

后端 未结 3 1001
野性不改
野性不改 2020-11-29 21:21

SharedPreferences have method getAll, but it returns no entries despite the fact some keys exist:

PreferenceManager.getDefaultSharedPreferences(this).contain         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 21:58

    What you can do is use getAll() method of SharedPreferences and get all the values in Map and then you can easily iterate through.

    Map keys = prefs.getAll();
    
    for(Map.Entry entry : keys.entrySet()){
                Log.d("map values",entry.getKey() + ": " + 
                                       entry.getValue().toString());            
     }
    

    For more you can check PrefUtil.java's dump() implementation.

提交回复
热议问题