开发者

How save two webview in SaveInstanceState?

First i will paste my code.

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

Now is my question. I have two webview widget. One is called webview and another webviewtwo. I'm newbie in android programing so i have problem how save two widgets in onSaveInstanceState and how restore them. Now webviewtwo overwrite webview and in webview windows i have page from webviewtwo. How fix it and do it right ?

Sierran


It's my understanding that onSaveInstanceState should be used to persist any dynamic state information needed to rebuild the application. You wouldn't want to persist the actual widget but rather the information that the widget needs to restore itself.


onSaveInstanceState takes only one Bundle.

Save the webview states into separate Bundles, then put them into the saved one:

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

Restoring:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜