我有一个主要活动有两个标签,Tab1和Tab2。
Tab1有一个ListFragment。 Tab2有一个Webview Fragment。
如果我在ListFragment上选择一个列表项,它将导航到DetailFragment。然后,如果我单击Tab2然后按Tab1,我仍然会查看DetailFragment。但如果我按回这里,会出现一个空白片段。
我该如何解决?
EDIT。一些代码:
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_main_view);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "inbox");
mTabHost.addTab(mTabHost.newTabSpec("inbox").setIndicator("", getResources().getDrawable(R.drawable.ac_tab_indicator_inbox)), InboxFragment.class, b);
//
b = new Bundle();
b.putString("key", "ac_app");
mTabHost.addTab(mTabHost.newTabSpec("ac_app").setIndicator("", getResources().getDrawable(R.drawable.ac_tab_indicator_wall)), ACFragment.class, b);
TabWidget tabWd = (TabWidget) findViewById(android.R.id.tabs);
if (tabWd != null) {
tabWd.setBackgroundColor(getResources().getColor(R.color.light_gray));
}
}
public void navigateToDetail(ACMessage msg) {
InboxDetailMessageFragment frDetail = new InboxDetailMessageFragment();
frDetail.setMsg(msg);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.left_to_rigth, R.anim.right_to_left, R.anim.left_to_rigth_out, R.anim.right_to_left_out);
ft.replace(R.id.realtabcontent, frDetail);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
}
}
答案 0 :(得分:2)
您是否可能初始化了比您显示的标签更多的标签?这可能是您获得空白页面背后的原因。对于片段不切换部分,您是否正确使用了必要的回调方法?
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
mActionBar.setSelectedNavigationItem(position); //mTabHost in this case maybe
}
此外,您可以检查onTabSelected()方法
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
Context context;
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
mActionBar.setTitle(tab.getContentDescription());
System.out.println(tab.getContentDescription());
}
}