按下后退按钮时未加载新首选项
我有这个首选项类(如下),它保存两个 ListPreferences,但如果更改 ListPreferences 并按下后退按钮,则除非重新启动应用程序,否则更改不会生效。我错过了什么吗?一直在到处寻找,但似乎找不到合适或有效的答案。请帮忙。
public class Preferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}
应用程序代码
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String q = SP.getString("appViewType","http://www.google.com");
String c = SP.getString("appRefreshRate","20");
webview = (WebView) findViewById(R.id.scroll);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new QuotesWebView(this));
webview.loadUrl(q);
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
webview.reload();
}
}, 10, Long.parseLong(c),TimeUnit.SECONDS);
findViewById(R.id.refresh).setOnClickListener(this);
}
@Override
public void onPause(){
super.onPause();
}
@Override
public void onResume(){
super.onResume();
}
public void onClick(View v){
switch(v.getId()){
case R.id.refresh:
webview.reload();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem about = menu.getItem(0);
about.setIntent(new Intent(this, About.class));
MenuItem preferences = menu.getItem(1);
preferences.setIntent(new Intent(this, Preferences.class));
return true;
}
}
I have this preferences class (below) that saves two ListPreferences, but if the ListPreferences are changed and the back button is pressed, the changes don't take affect unless the application is restarted. Did I miss something? Have been looking everywhere, but just can't seem to find an answer the fits or works. Please help.
public class Preferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}
Application Code
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String q = SP.getString("appViewType","http://www.google.com");
String c = SP.getString("appRefreshRate","20");
webview = (WebView) findViewById(R.id.scroll);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new QuotesWebView(this));
webview.loadUrl(q);
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
webview.reload();
}
}, 10, Long.parseLong(c),TimeUnit.SECONDS);
findViewById(R.id.refresh).setOnClickListener(this);
}
@Override
public void onPause(){
super.onPause();
}
@Override
public void onResume(){
super.onResume();
}
public void onClick(View v){
switch(v.getId()){
case R.id.refresh:
webview.reload();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem about = menu.getItem(0);
about.setIntent(new Intent(this, About.class));
MenuItem preferences = menu.getItem(1);
preferences.setIntent(new Intent(this, Preferences.class));
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当首选项活动完成时,您需要以某种方式重新加载您的首选项。我认为 Dirol 在 onResume() 而不是 onCreate() 中加载它们的建议非常好;你尝试过吗?或者我也误解了这个问题。
就我自己而言,我使用 startActivityForResult() 启动了首选项活动,然后在活动结果回调中,我重新加载了首选项。
代码片段:
无论如何,这对我有用。我可以尝试将 updatePreferences() 调用移至 onResume() ,看看是否也有效。
You need to somehow reload your preferences when the preferences activity finishes. I thought Dirol's suggestion of loading them in onResume() instead of onCreate() was excellent; have you tried it? Or am I misunderstanding the problem as well.
In my own case, I launched the preferences activity with startActivityForResult() and then on the activity result callback, I reloaded the preferences.
Code snippets:
Anyway, this is what works for me. I may try moving my updatePreferences() call to onResume() myself to see if that works too.
尝试重写 onBackPressed() 方法。
如果“向上”按钮(左上角 <-)提供了正确的结果,那么您可以将“后退”按钮设置为与“向上”按钮类似。
Try overriding the onBackPressed() method.
If your "Up" button (top left <-) provides the correct result, then you can set the Back button to behave like the Up button.
您仅在 onCreate() 方法上加载首选项。该方法仅在新活动启动时调用。 addPreferencesFromResource 将 xml 文件填充到首选项中,因此您只能获取信息,该信息在调用 addPreferencesFromResource 时已经存储在 xml 中,而不是之后。
尝试将该方法移至 onResume。但要注意内存泄漏。我不知道 addPreferencesFromResource 到底做了什么,但从文档来看 - 我对该方法活动非常怀疑。
You load preferences only on onCreate() method. That method called only when a fresh activity starts up. The addPreferencesFromResource inflates the xml file into the preferences, so you only get the info, which is already has been stored in the xml at the moment addPreferencesFromResource was called, not after.
Try to move that method to onResume. But watch for the memory leak. I don't know exactly what the addPreferencesFromResource do, but from the documentation - I would be very suspicious about that method activity.
我遇到了同样的问题,并按如下方式解决:
主活动类实现 OnSharedPreferenceChangeListener:
在主活动类内部,只要首选项条目发生更改,就会运行 onSharedPreferenceChanged。我只是像在 onCreate 中那样更新首选项中的所有变量:
这可以解决问题,我希望它可以为您节省一些寻找解决方案的时间。
I had the same problem and solved it as follows:
The main activity class implements OnSharedPreferenceChangeListener:
Inside the main activity class the onSharedPreferenceChanged is run whenever a preference entry changes. I simply update all my variables from the preferences as i did in onCreate:
This does the trick and I hope it saves you some time in searching for a solution.
我也遇到过同样的问题...
尝试创建首选项实例并在您需要的每个类和每个活动中加载其数据。
它对我有用......希望它有帮助。
I've had the same problem...
Try to create preference instance and load its data in every class and every activity where you need it.
It worked for me...Hope it helps.
您将需要重新加载视图或使用这些首选项的任何对象,最好是在首选项活动关闭时。
首选项活动不会更改任何内容,只会更改包含您的首选项的内部文件(键=值列表)。当它发生更改时,
preferenceActivity
会调用onPreferenceChaged()
,仅此而已。它本身不会刷新你的东西。您需要重新加载首选项并在 onResume() 方法或等效方法中重用它们。You will need to reload your view or whatever object which uses those preferences, preferably when preference activity closes.
Preference activities do not change nothing but an internal file with your preferences(key=value list). When it is changed,
preferenceActivity
callsonPreferenceChaged()
and nothing more. It doesn't refresh your stuff by itself. You need to reload prefs and to reuse them inonResume()
method or equivalent.