我使用FragmentTabHost和TabWidget创建4个标签。选择某个选项卡后,它会从其相关类中打开其相关的xml文件。假设我选择了标签1.它在网页浏览中打开了一个网站。然后我移动到下一个选项卡,即选项卡2.当我回到选项卡1时,它重新绘制webview并再次请求该网站。它像以前一样需要与同一页面一样无用。我的意思是不保留tab的最后状态。它从头开始。如果我离开标签1,如何获取网页的最后内容?这是代码:
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "Home");
mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"),
Home.class, b);
//
b = new Bundle();
b.putString("key", "Menu");
mTabHost.addTab(mTabHost.newTabSpec("menu").setIndicator("Menu"),
MenuDemo.class, b);
b = new Bundle();
b.putString("key", "Promos");
mTabHost.addTab(mTabHost.newTabSpec("promos").setIndicator("Promos"),
Promos.class, b);
b = new Bundle();
b.putString("key", "Promos");
mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator("Location"),
Location.class, b);
标签1打开此活动:
public class Home extends Fragment{
public Home() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.home,
null);
WebView webView = (WebView) v.findViewById(R.id.webViewHome);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
return v;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
//
}
答案 0 :(得分:0)
在你的mainTab Activity中你应该做这样的事情来隐藏片段并检查它是否已经加载只显示它:
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
// ft.detach(mLastTab.fragment);
ft.hide(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
// ft.attach(newTab.fragment);
ft.show(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
并将标签的标签发送给该标签。
希望有所帮助:)