I have a webview as the main activity and I want refresh the webview from the "Preference" (listview) that is from another activity. Is it possible to do that? I don't want to have the "refresh" button on the app开发者_开发问答s menu.
But the activity will crash after i pressed "refresh" on my preference activity , i assume that is "R.id.web_engine" , that is from the MainActivity layout , that causes the crash (look at the code below).
How can I perform a webview action from the external activity ?
Example :
In my MainActivity will have
//Webview
final WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("file:///android_asset/www/index.html");
engine.getSettings().setJavaScriptEnabled(true);
and Preferences
//Get the custom preference
Preference refreshPref = (Preference) findPreference("refreshPref");
refreshPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getBaseContext(),
"Loading...",Toast.LENGTH_SHORT).show();
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("javascript:window.location.reload();");
return true;
}
});
This line is causing the crash
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("javascript:window.location.reload();");
It should be a null pointer crash if I understand correctly since this Preference activity will never be able to find a WebView which is main activity using this method. The better way to do this would be to broadcast an intent to refresh and handle that intent in main activity and perform the action.
See details about broadcast receivers here http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
I agree with Rahul Choudhary. Your webview is only visible from your main activity. You have to pass it a message that it should refresh next time it is visible.
The right place for this code is on the main activity onResume() method. You can use intents to launch it again (it will be recreated and loose its history) or use SharedPreferences that are accessible by both activities.
EDIT: added some code below.
MainActivity
//onCreate
final WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("file:///android_asset/www/index.html");
engine.getSettings().setJavaScriptEnabled(true);
//onResume
boolean refresh = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("SHOULD_REFRESH", false)
if (refresh) {
engine.loadUrl("javascript:window.location.reload();");
//remove the shared preference here or set it to false to prevent reloading next time
}
and Preferences
//Get the custom preference
Preference refreshPref = (Preference) findPreference("refreshPref");
refreshPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("SHOULD_REFRESH", true).commit();
return true;
}
});
精彩评论