当我在shouldOverrideUrlLoading中启动活动GameActivityLandscape时,我已将方向MainWebActivity设置为PORTRAIT,将GameActivityLandscape设置为AndroidManifest.xml中的LANDSCAPE,但是当我按下GameActivityLandscape中的后退按钮时,会出现一个空白页面,我希望MainWebActivity像第一次加载< / p>
我的MainWebActivity是
public class MainWebActivity extends Activity {
private WebView myWebView;
ProgressBar progressBar;
boolean internet;
Bundle webState=null;
String GMTag="MainWebActivity ";
static String lodUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
myWebView = (WebView) findViewById(webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new myWebClient());
myWebView.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
//myWebView.clearCache(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
if (savedInstanceState == null) {
Log.d(GMTag, " onResume webview load ");
myWebView.loadUrl("http://html5games.com/Game/Civilizations-Wars-Master-Edition/2001681a-0f53-4691-9318-04e419ac7c0c");
}
}
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.d(GMTag, "Main Web shouldOverrideUrlLoading test" + url.toString());
return handleUri(url);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(GMTag, "Main Web android N shouldOverrideUrlLoading " + request.getUrl().toString());
//final Uri uri = request.getUrl();
return handleUri(request.getUrl().toString());
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
/*public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(MainWebActivity.this, "Your Internet Connection May not be active Or " + error.getDescription() , Toast.LENGTH_LONG).show();
}*/
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(errorCode==404){
Toast.makeText(MainWebActivity.this, "File or directry not found" , Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainWebActivity.this, "Your Internet Connection May not be active Or " + description, Toast.LENGTH_LONG).show();
}
}
}
private boolean handleUri(String url) {
if (url.contains("fg_domain=play.famobi.com&fg_aid=A1000-1&fg_uid=2001681a-0f53-4691-9318-04e419ac7c0c&fg_pid=4638e320-4444-4514-81c4-d80a8c662371&"))
{
Intent myIntent = new Intent(MainWebActivity.this, GameActivityLandscape.class);
myIntent.putExtra("URL", url); //Optional parameters
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
MainWebActivity.this.startActivity(myIntent);
progressBar.setVisibility(View.GONE);
} else {
myWebView.loadUrl(url);
}
return true;
}
@Override
protected void onPause() {
super.onPause();
myWebView.onPause();
Log.d(GMTag, " Web onPause called " + webState);
}
@SuppressLint("SetJavaScriptEnabled")
@JavascriptInterface
@Override
protected void onResume() {
super.onResume();
myWebView.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(GMTag, " onSaveInstanceState ");
super.onSaveInstanceState(outState);
webState=outState;
myWebView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(GMTag, " webview restored in onRestoreInstanceState ");
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@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);
}
}
这是我的GameActivityLandscape活动
public class GameActivityLandscape extends Activity {
private WebView myWebView;
static String gameRotation, gameUrl;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Intent intent = getIntent();
gameRotation = intent.getStringExtra("ORIENTATION");
gameUrl = intent.getStringExtra("URL");
//progressBar=(ProgressBar)findViewById(R.id.game_progressBar);
myWebView = (WebView) findViewById(R.id.game_webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new myWebClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (savedInstanceState == null) {
myWebView.loadUrl(gameUrl);
}
}
public class myWebClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.d("Game", "onError landscape called " + failingUrl);
}
}
@Override
protected void onResume() {
super.onResume();
myWebView.onResume();
Log.d("Game", "onresume called of gameacticity landcape ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("Game", "onPause called of gameacticity landcape ");
myWebView.onPause();
}
@Override
public void onBackPressed() {
super.onBackPressed();
Log.i("Gaming mania", "Play game landscape activity finish");
finish();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}