如何在SavedInstanceState中保存两个webview?

时间:2011-10-05 14:50:09

标签: java android

首先我会粘贴我的代码。

public class ZSEEActivity extends TabActivity {
private WebView webview ; 
private WebView webviewtwo;




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Activity activity = this;


    TabHost 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));

    mTabHost.setCurrentTab(0);


    webview = (WebView) findViewById(R.id.webView1);
    webviewtwo = (WebView) findViewById(R.id.webView2);
    WebSettings webviewtwoSettings = webviewtwo.getSettings();
        if (savedInstanceState != null){
          webview.restoreState(savedInstanceState);
          webviewtwo.restoreState(savedInstanceState);
        }
        else{
            webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
            webviewtwoSettings.setDefaultFontSize(30);
            webviewtwo.loadUrl("http://zsee.bytom.pl/plany/index.html");
        }


    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) {
           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();
       }
     });

    }
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

protected void onSaveInstanceState(Bundle outState, Bundle Test) {
    webview.saveState(outState);
    webviewtwo.saveState(Test);
}



public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.item1:
        AlertDialog alertdialog= new AlertDialog.Builder(this).create();
        alertdialog.setTitle("Pomoc");
        alertdialog.setMessage("Lepszy Planer od Sierran'a :>");
        alertdialog.show();
        return true;
    case R.id.item2:
        finish();
    case R.id.item3:
        webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
        System.out.print("ss");
    default:
        return super.onOptionsItemSelected(item);
    }
}
}

现在是我的问题。我有两个webview小部件。一个叫做webview,另一个是webview。我是android编程的新手所以我有问题如何在onSaveInstanceState中保存两个小部件以及如何恢复它们。现在webviewtwo覆盖webview,在webview窗口我有来自webviewtwo的页面。如何修复并正确行事?

Sierran

2 个答案:

答案 0 :(得分:2)

我的理解是应该使用onSaveInstanceState来保存重建应用程序所需的任何动态状态信息。您不希望持久保存实际的窗口小部件,而是希望窗口小部件需要自我恢复的信息。

答案 1 :(得分:1)

onSaveInstanceState只接受一个Bundle。

将webview状态保存到单独的Bundles中,然后将它们放入已保存的Bundles中:

Bundle state1=new Bundle();
webview.saveState(state1);
Bundle state2=new Bundle();
webviewtwo.saveState(state2);
outState.putBundle("state1",state1);
outState.putBundle("state2",state2);
super.onSaveInstanceState(outState);

恢复:

webview.restoreState(savedInstanceState.getBundle("state1"));
webviewtwo.restoreState(savedInstanceState.getBundle("state2"));