James James - 1 month ago 11
Android Question

Android Web App breaks on Screen Rotation After Invalid Login

I am making a simple Android app that essentially redirects to a

WebApp
. To do this, I use a
WebView
. I am getting an error "net:::ERR_CACHE_MISS" only when there is a failed login attempt followed by an orientation change. Does anyone know why this could be? I am nearly positive it is not to do with the
WebApp
I am redirecting to.

Code below:

public class MainActivity extends Activity {

WebView myWebView;
String webURL = "omitted";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});

if (savedInstanceState == null) {
myWebView.loadUrl(webURL);
}
else {
((WebView) findViewById(R.id.webview)).restoreState(savedInstanceState);
}

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}

return super.onKeyDown(keyCode, event);
}

@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}


}

Answer

Put this

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

In your activity tag, in the AndroidMainfest.xml