0

I have a main activity with two tabs, Tab1 and Tab2.

Tab1 has a ListFragment. Tab2 has a Webview Fragment.

If I select a list item on ListFragment, it navigates to DetailFragment. Then, if I click on Tab2 and then press Tab1, I still viewing DetailFragment. But if I press back here, a blank fragment appears.

How can I solve it?


EDIT. Some code:

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

}

user2655890
  • 1
  • 1
  • 4

1 Answers1

2

Is it possible that you have initialized more tabs than those you are showing? It may be the reason behind you getting the blank page. As for the fragment not switching part, have you used the necessary callback methods properly?

@Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        mActionBar.setSelectedNavigationItem(position); //mTabHost in this case maybe
    }

Also, you can check the onTabSelected() method

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());
        }
    }
SVG
  • 123
  • 9