I got a webview which is implemented like that:
this.webView = (WebView) view.findViewById(R.id.typeform);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClientHelper(activity));
I don't want to reload the webpage if I click back button and then come to the fragment again, so I implemented that:
In the onCreateView:
if (webViewBundle == null) {
webView.loadUrl(Constants.TYPEFORM_URL);
} else {
webView.restoreState(webViewBundle);
}
In the onPause():
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
Not sure why it does not work. Any recommendation?
Thanks in advance.
If you are getting to onCreateView when you click the back button, it means you are creating a new view instance every time. You need to have one instance, and if you want to override the behavior when you come back to it, implement onResume.
Related
I made a url to open through webview and everything worked fine for me except some buttons of the webpage it doesn't respond anything
public class browse extends AppCompatActivity {
WebView appwebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse);
String profileID= getIntent().getStringExtra("inputtext");
Toast.makeText(getApplicationContext(),profileID,Toast.LENGTH_SHORT).show();
appwebview=(WebView) findViewById(R.id.webview);
WebSettings webSettings= appwebview.getSettings();
webSettings.getJavaScriptEnabled();
appwebview.loadUrl("https://statsroyale.com/profile/"+profileID);
appwebview.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(appwebview.canGoBack())
appwebview.goBack();
else
super.onBackPressed();
}
}
the intent passes text of textfield from MainActivity and depending on that text i used to show profile.
Probably you forgot to activate Javascript in the WebView.
Please try this:
webSettings.setJavaScriptEnabled(true);
instead of:
webSettings.getJavaScriptEnabled();
A couple of other settings could help:
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setDomStorageEnabled(true);
Add this code to appwebview when it finishes loading as sometimes javascript interaction does not work properly.
appwebview.requestFocus();
I have this code which works and the only problem with it is that if I leave the WebView and even close the app, and even press power on the phone, the audio from the podcasts keeps playing.
Would anyone know how to stop that?
Here is the code:
public class PodcastsActivity extends BaseActivity //implements ActionBar.OnNavigationListener
{
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
}
// public void onBackPressed ( )
// {
// Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
// invoke(html5WebView, (Object[]) null);
// }
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onStop()
{
super.onStop();
// your code
webview.clearView();
}
You should call through to the WebView's onPause() and onResume() from your Activity's onPause() and onResume(), respectively.
Pauses any extra processing associated with this WebView and its
associated DOM, plugins, JavaScript etc. For example, if this WebView
is taken offscreen, this could be called to reduce unnecessary CPU or
network traffic. When this WebView is again "active", call onResume().
There's also pauseTimers(), which affects all of the WebViews within your application:
Pauses all layout, parsing, and JavaScript timers for all WebViews.
This is a global requests, not restricted to just this WebView. This
can be useful if the application has been paused.
Accepted answer didnt work for me - just using onPause in a fragment was not enough to stop music on Android 9. I had to also do:
override fun onDestroyView() {
webview?.destroy()
super.onDestroyView()
}
Same issue for the Jetpack Compose can be solved by capturing the WebView (https://google.github.io/accompanist/webview/) in "onCreated" and then use it to call "destroy()"
val webViewState = rememberWebViewState(webViewUrl.value)
val webView = remember { mutableStateOf<android.webkit.WebView?>(null) }
//..inside Composable..
WebView(state = webViewState,
captureBackPresses = true,
onCreated = { wv ->
webView.value = wv
}
)
//..on Back Press/Close Button..
webView.value?.destroy()
In my case WebView onPause() and onResume() is not working for me.
Solution:
onPause I load the below URL to stop the audio:
webView.loadUrl("javascript:document.location=document.location");
I am using webview in my android app. It loads a webpage successfully.When I click on a link, it opens the new page in the webview and not in browser. Now my problem is that on tilting the screen the page gets refreshed and again the homepage is loaded instead of the page where we were earlier. How can I overcome this problem? Thanks in advance..my code is given below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.xyz.com");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new MyWebViewClient());
}
my webviewclient class:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}}
Well, that's because when the orientation changes, the activity is finished and restarted. So the onCreate() is called again and the first URL is loaded. You will have to save the state of the webview and restore it on orientation change.
I'm late in the conversation, but this idea is really interesting too:
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
Basically:
1 - You make use of savedInstanceState
2 - You create your webView programmatically based on your savedInstanceState so that there is no reloading, just the exact copy of the original webView when tilting.
In a WebView, I'm trying to load a html5 page where I embed a <video> but is there any way I can detect when the user selects the play button or the video starts playing?
Right now is when I play the video, default is it fires an Intentto open Android's default player, I don't want it to fire an Intent, I want to use a Dialog and play it there. If anybody can give an idea. Thanks.
#Override
public void onCreate(Bundle savedInstanceState) {
....
mywebviewer = (WebView) findViewById(R.id.mywebviewer);
WebSettings webSettings = mywebviewer.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebviewer.loadUrl("https://www.youtube.com");
mywebviewer.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (mywebviewer.canGoBack()) {
mywebviewer.goBack();
} else {
super.onBackPressed();
}
}
I do it in this way:
boolean videoPlay=false;
webView.setWebChromeClient(new WebChromeClient() {
#Override
public Bitmap getDefaultVideoPoster() {
super.getDefaultVideoPoster();
videoPlay=true;
Log.d("vplayyyyyyyy",String.valueOf(videoPlay));
return null;
}
});
hope this help :)
I am doing one app. In this I am passing html file using WebView it is working good. But in html page I have some links. When I click that link means it is default going to website. But when I click that time no need to go to website. I need to go some activity like OnlineQuery.java. But I dnt knw how to move to that java file. any one knows please help me.
Java file:
public class DrugOffences extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setBackgroundColor(Color.BLACK);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/Drug.html");
}
}
Set a WebViewClient to your webview and override shouldOverrideUrlLoading. Check if the url to be loaded is the one you want to intercept. If it is do what you want and return true, if its not, return false.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("theURLYouDontWantToLoadInBrowser")) {
//Do your thing
startActivity(new Intent(this, OnlineQuery.class));
return true;
} else {
return false;
}
});
I don't understand at all your question, but if what you want is to handle the url navigation of the html load in the java code (webview), you could implement shouldOverrideUrlLoading (in this method you can intercept the url loadings) function.
Hope this helps
Try this..
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);