Reputation: 3807
I am Using Following Code to stop reloading the web page
public class MainActivity extends Activity {
WebView webView;
@Override
protected void onSaveInstanceState(Bundle outState) {
WebView webView1 = (WebView)findViewById(R.id.webView);
webView1.saveState(outState);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView);
String url="http://www.google.com";
if (savedInstanceState != null)
{
((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState);
}
else{
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String name = MainActivity.this.webView.getTitle();
TextView t=(TextView)findViewById(R.id.title);
t.setText(name);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
});
final Activity activity = this;
final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setUserAgentString("Android");
webView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress) {
progressBar.setVisibility(View.VISIBLE);
progressDialog.setProgress(0);
activity.setProgress(progress * 1000);
progressDialog.incrementProgressBy(progress);
if(progress > 75)
progressBar.setVisibility(View.GONE);
}
}
);
webView.loadUrl(url);
}
and used android:configChanges="orientation|keyboard|keyboardHidden"
in my manifest file
But when i run it and change orientation of my phone.
The page still get reloaded and progressbar started showing and never dismiss.
can anyone tell me whats wrong in this??
Upvotes: 1
Views: 2120
Reputation: 799
Try this
in activity tag, manifest
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize"
java
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// We do nothing here. We're only handling this to keep orientation
// or keyboard hiding from causing the WebView activity to restart.
}
Upvotes: 1
Reputation: 671
In my code where WebView is in fragment:
In manifest:
<activity
...
android:configChanges="orientation|keyboard|screenSize"
... />
In activity:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
In fragment:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null)
webView.loadUrl("about:blank");
else
webView.restoreState(savedInstanceState);
}
and when orientation change it's change smoothly.
Upvotes: 0
Reputation: 992
Please add screenSize also in the manifest android:configChanges="orientation|keyboard|screenSize"
Upvotes: 1
Reputation: 5784
just put this in your manifest activity
android:configChanges="keyboardHidden|orientation"
it's working for me
Upvotes: 1
Reputation: 2158
You need to use configChanges attribute for for Activity in manifest file. So use below code in your Manifest file for each activity.
<activity android:name=".YOUR_ACTIVITY_NAME" android:configChanges="orientation|keyboard|keyboardHidden"/>
When you are not using configChanges then onCreate()
method again called when device orientation change or some other configuration changes.
Upvotes: 0
Reputation: 18670
By default the activity is recreated at orientation change. But you can change it by setting android:configChanges="keyboardHidden|orientation
in the declaration of your activity in AndroidManifest.xml and overriding the onConfigurationChanged()
method of the activity class.
Upvotes: 1