我已经实现了一个tabHost,它托管了一些动态创建的webview。我想要的行为如下:
因此,我想要的行为实际上就像一个支持多个标签的简单浏览器。
我的代码是:
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
public TabActivity act=this;
private String startUrl="myUrl";
public ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
public int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createTab(startUrl);
}
private void createTab(String url) {
TabHost tabs = getTabHost();
TabSpec spec=tabs.newTabSpec(Integer.toString(counter));
spec.setIndicator(Integer.toString(counter));
counter++;
spec.setContent(new MyTabContentFactory(url));
list.add(spec);
tabs.addTab(spec);
tabs.setCurrentTabByTag(spec.getTag());
}
public class MyTabContentFactory implements TabContentFactory{
private String url;
public MyTabContentFactory(String url) {
this.url=url;
}
@Override
public View createTabContent(String tag) {
WebView browser=new WebView(act);
browser.setWebViewClient(new MyWebViewClient());
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setBuiltInZoomControls(true);
browser.loadUrl(url);
return (browser);
}
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
createTab(url);
return false;
}
}
@Override
public void onBackPressed() {
TabHost tabs = getTabHost();
int id=tabs.getCurrentTab();
list.remove(id); // remove it from memory
tabs.clearAllTabs(); // clear all tabs from the tabhost
if(list.size()>0)
for(TabHost.TabSpec spec : list) // add all that you remember back
tabs.addTab(spec);
else
act.finish();
}
}
此代码的行为是:
我整天都在努力解决这个问题,我无法弄清楚我的错误在哪里......有人能帮助我吗? 也建议一个很好的方法来实现这一点,将不胜感激!
谢谢!
编辑: 现在我的代码
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private final static String TAG="MainActivity";
public TabActivity act=this;
private final static String startUrl="http://www.myurl.com";
private ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
private int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG,"onCreate");
if(list.size()==0)
createTab(startUrl);
else{
TabHost tabs = getTabHost();
tabs.clearAllTabs(); // clear all tabs from the tabhost
for(TabHost.TabSpec spec : list) // add all that you remember back
tabs.addTab(spec);
}
}
@Override
protected void onStart(){
super.onStart();
Log.i(TAG,"onStart");
}
@Override
protected void onRestart(){
super.onRestart();
Log.i(TAG,"onRestart");
}
@Override
protected void onResume(){
super.onResume();
Log.i(TAG,"onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.i(TAG,"onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.i(TAG,"onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"onRestoreInstanceState");
}
private void createTab(String url) {
Log.i(TAG,"createTab");
TabHost tabs = getTabHost();
TabSpec spec = tabs.newTabSpec(Integer.toString(counter));
spec.setIndicator(Integer.toString(counter));
spec.setContent(new MyTabContentFactory(url));
list.add(spec);
tabs.addTab(spec);
tabs.setCurrentTab(counter);
counter++;
}
@Override
public void onBackPressed() {
Log.i(TAG,"onBackPressed");
TabHost tabs = getTabHost();
int id=tabs.getCurrentTab();
list.remove(id); // remove it from memory
counter--;
tabs.clearAllTabs(); // clear all tabs from the tabhost
int indicator=0;
if(list.size()>0)
for(TabHost.TabSpec spec : list){ // add all that you remember back
spec.setIndicator(Integer.toString(indicator));
indicator++;
tabs.addTab(spec);
}
else
act.finish();
}
public class MyTabContentFactory implements TabContentFactory{
private String url;
public MyTabContentFactory(String url) {
this.url=url;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View createTabContent(String tag) {
WebView browser=new WebView(act);
browser.setWebViewClient(new MyWebViewClient());
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT>10)
browser.getSettings().setDisplayZoomControls(false);
browser.loadUrl(url);
return (browser);
}
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG,"shouldOverrideUrlLoading");
Log.i(TAG,url);
createTab(url);
return true;
}
}
}
一切正常,但我不知道如何在屏幕方向改变时存储webview状态......任何提示?