更改屏幕方向后的webview重新加载页面之一

时间:2011-10-19 17:55:00

标签: java android

第一个代码:

package com.fshare.zsee;


public class ZSEEActivity extends TabActivity {
private WebView webview ; 
private WebView webviewtwo;
private TabHost mTabHost;
private WebSettings webviewtwoSettings;
private int error;


protected void onStart() {
    super.onStart();
    // The activity is about to become visible.
}
protected void onStop() {
    super.onStop();
    // The activity is about to become visible.
}
protected void onRestart() {
    super.onRestart();
}
protected void onDestroy(){
    super.onDestroy();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Activity activity = this;


    mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Zastępstwa").setContent(R.id.tab1));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Plan Lekcji").setContent(R.id.tab2));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("O programie").setContent(R.id.tab3));



    webview = (WebView) findViewById(R.id.webView1);
    webviewtwo = (WebView) findViewById(R.id.webView2);
    webviewtwoSettings = webviewtwo.getSettings();
        if (savedInstanceState != null){
            error = savedInstanceState.getInt("webtwoerror");
            webview.restoreState(savedInstanceState.getBundle("stateone"));
            webviewtwo.restoreState(savedInstanceState.getBundle("statetwo"));
            if(error == 1){
                webviewtwoSettings.setTextSize(TextSize.NORMAL);
            }
            else{
                webviewtwoSettings.setTextSize(TextSize.LARGER);    
            }
            mTabHost.setCurrentTab(savedInstanceState.getInt("CURRENT_TAB"));

        }
        else{
            webviewtwoSettings.setTextSize(TextSize.LARGER);
            webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
            webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
            //error = 0 ;
            mTabHost.setCurrentTab(0);
        }

    webview.setWebViewClient(new WebViewClient() {
           public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
               webview.loadData(summary, "text/html","utf-8");
             Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
           }
         });

    webviewtwo.setWebViewClient(new WebViewClient() {
       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
           webviewtwoSettings.setTextSize(TextSize.NORMAL);
           String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
           webviewtwo.loadData(summary, "text/html","utf-8");
         Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
         error = 1 ;
       }
     });

    }
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}
@Override     
public boolean onKeyDown(int keyCode, KeyEvent event) 
 {         
   if (keyCode == KeyEvent.KEYCODE_BACK) 
      {            
    // put your stuff here or just block the back button for perticular activity             
         Intent i = new Intent();
        i.setAction(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
        this.startActivity(i);     
      }         
    return super.onKeyDown(keyCode, event); 
  }


protected void onSaveInstanceState(Bundle outState) {
    Bundle outStateone = new Bundle();
    Bundle outStatetwo = new Bundle();
    webview.saveState(outStateone);
    webviewtwo.saveState(outStatetwo);


    outState.putBundle("stateone", outStateone);
    outState.putBundle("statetwo", outStatetwo);
    outState.putInt("CURRENT_TAB", mTabHost.getCurrentTab());
    outState.putInt("webtwoerror", error);
}



public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.item1:
        final AlertDialog alertdialog= new AlertDialog.Builder(this).create();
        alertdialog.setTitle("O Programie");
        alertdialog.setMessage("Zmiany w 1.0.1: \n-Obsługa planu z dnia 17.10.2011\n-Drobne Poprawki");
        alertdialog.setButton("Fajno", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                alertdialog.cancel();
           }
       });
        alertdialog.show();
        return true;
    case R.id.item2:
        finish();
    case R.id.item3:
        if(mTabHost.getCurrentTab() == 0){
        webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
        }
        else if(mTabHost.getCurrentTab() == 1)
        {
        error = 0 ;
        webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
        webviewtwoSettings.setTextSize(TextSize.LARGER);
        }
    default:
        return super.onOptionsItemSelected(item);
    }
}
}

现在非常简单的问题。为什么webviewtwo在更改方向和webview(webView1)之后重新加载页面?以及如何预防?现在只有webView1在更改屏幕方向后才会重新加载页面。

Sierran

1 个答案:

答案 0 :(得分:0)

我不确定为什么第一个没有改变,但是将它添加到我为我工作的活动标签内的清单

android:configChanges="orientation|keyboardHidden|keyboard"