[英]Preference in PreferenceFragment is not getting updated
I have a main activity that loads a PreferenceFragment (its part of an actionBar). 我有一个主要的活动,该活动会加载PreferenceFragment(它是actionBar的一部分)。
Within the PreferenceFragment I load my preferences from a XML File: 在PreferenceFragment中,我从XML文件加载了我的首选项:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
The Problem is if I change the Tab (remember it is getting managed by an ActionBar), change one of the preferences in a different fragment and coming back to my preferenceFragment its not getting updated. 问题是如果我更改了选项卡(请记住它正由ActionBar进行管理),更改了另一个片段中的首选项之一,并返回到我的preferenceFragment中,它没有被更新。 Particularly the problem is a SwitchPreference(true/false) which can be changed from different fragments and even by remote calls (then the preference is getting changed in shared preferences. YES I did commit the change).
尤其是问题是可以从不同片段甚至通过远程调用更改SwitchPreference(true / false)(然后在共享首选项中更改了首选项。是的,我确实提交了更改)。
I searched for different solutions, but to be honest I didn´t find a working one. 我搜索了不同的解决方案,但老实说,我没有找到可行的解决方案。 My own ideas are the following:
我自己的想法如下:
My Problem would be solved if I could get the Switch Element of the switch, so I could set the switch to true in the onStart() or onResume() method. 如果可以获取开关的Switch元素,则将解决我的问题,因此可以在onStart()或onResume()方法中将开关设置为true。 But is there a change to get the Switch Object?
但是,获取Switch对象是否有变化? If I would load a usual layout I could access the switch like this:
如果我要加载常规布局,则可以这样访问该开关:
View v = inflater.inflate(R.layout.start_fragment, container, false);
Switch status = (Switch) v.findViewById(R.id.switch1);
Afterwards I would be able to set the Position of the Switch like this: 之后,我将能够像这样设置开关的位置:
status.setChecked(true);
Another solution would be to destroy the actual view and call addPreferencesFromResource() again, but to be honest I have no idea how this could be done.... 另一个解决方案是销毁实际视图并再次调用addPreferencesFromResource(),但是老实说,我不知道该怎么做。
The third solution could be to use a OnCheckedChangeListener in my PreferenceFragment, but the problem would again be how to change/update the switch. 第三种解决方案可能是在我的PreferenceFragment中使用OnCheckedChangeListener,但是问题仍然是如何更改/更新开关。
I am sure that the Preference is updated correctly, because I´m running a OnSharedPreferenceChangeListener in one of my services and to debug this problem I made the listener log the status of my switchPreference. 我确信首选项已正确更新,因为我正在其中一项服务中运行OnSharedPreferenceChangeListener,并且为了调试此问题,我使侦听器记录了switchPreference的状态。
Can you help me somehow? 你能以某种方式帮助我吗?
unfortunately I cannot answer my own question before tomorrow morning, so I edit my question: 不幸的是,我无法在明天早上之前回答自己的问题,所以我编辑了我的问题:
Thank you guys, I don´t want to restart the underlying activity, but only the fragment. 谢谢大家,我不想重新启动基础活动,而只想重新启动片段。
Fortunately I found a clue in this thread: How do you refresh PreferenceActivity to show changes in the settings? 幸运的是,我在此线程中找到了一个线索: 如何刷新PreferenceActivity以显示设置的更改?
I can really access the SwitchPreference that simple. 我真的可以这么简单地访问SwitchPreference。 My solution is:
我的解决方案是:
private SwitchPreference pref_phone_locked;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
status = (SwitchPreference) findPreference("status");
}
public void onStart(){
super.onStart();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
if(sharedPref.getBoolean("status", false)){
status.setChecked(true);
}else{
status.setChecked(false);
}
}
This way I can simply cast the Preference to a SwitchPreference and modify it after every onStart() call. 这样,我可以简单地将Preference转换为SwitchPreference,并在每次onStart()调用之后对其进行修改。 I guess this is the best solution for my problem.
我想这是解决我问题的最好方法。
Hopefully this answer will save someones time in future =) Thanks you guys again! 希望这个答案将来可以节省别人的时间=)再次谢谢你们!
I'm not sure if this will work for you ( I've a very basic knowlege of Android ) but I found this solution in a similar question and it works very well for me 我不确定这是否适合您(我有非常基础的Android知识),但是我在类似的问题中找到了该解决方案,对我来说效果很好
private void simulateRefresh(){
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
Basically this destroy and recreate the activity without animation in between. 基本上,这会破坏并重新创建活动,而中间没有动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.