0

Yesterday I downloaded new HoloEverywhere library. Currently, I have problem with tab navigation after screen rotation.

My Home Activity:

public class MainActivity extends Activity implements TabListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpTabs();
    }

private void setUpTabs() {
    String[] titles = {
            "First", "Second",
    };

    ActionBar supportActionBar = getSupportActionBar();

    for (int i = 0; i < titles.length; i++) {
        ActionBar.Tab tab = supportActionBar.newTab();

        tab.setText(titles[i]);
        tab.setTag(MyFragment.TAG);

        tab.setTabListener(this);
        supportActionBar.addTab(tab, false);

    }

    supportActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    supportActionBar.setSelectedNavigationItem(0);

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
    final String fragmentTag = tab.getTag().toString();
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
    if (fragment == null) {
        fragment = new MyFragment();
        fragmentTransaction.add(android.R.id.content, fragment, fragmentTag);
    } else {
        fragmentTransaction.attach(fragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
     Fragment fragment = getSupportFragmentManager().findFragmentByTag((String)     tab.getTag());

    if (fragment != null) {
        fragmentTransaction.detach(fragment);
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
} 

And my Fragment class.

public class MyFragment extends Fragment {
public static final String TAG = MyFragment.class.getCanonicalName();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = new View(getActivity());
    view.setBackgroundColor(Color.BLACK);
    return view;
}
}

When I rotate the screen fragment not displaying. It displays when i select tab (which is not currently selected) manually.

mmBs
  • 8,421
  • 6
  • 38
  • 46
lukjar
  • 7,220
  • 2
  • 31
  • 40
  • It was problem not related with HoloEverywhere. I should not use android.R.id.content. More information about this on [HoloEverywhere GitHub](https://github.com/Prototik/HoloEverywhere/issues/456) – lukjar May 23 '13 at 07:48

1 Answers1

0

I just solve the problem. I post my code here and see if those can help you :D

if (savedInstanceState == null){
    TabHomeFragment homeFragment = new TabHomeFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, homeFragment, "home_fragment").commit();
}else{
    TabHomeFragment homeFragment = (TabHomeFragment) getSupportFragmentManager().findFragmentByTag("home_fragment");
}

Those code are located in OnCreate method. When the Device rotate and Ortiention change, the fragment will recreate again. So add a if clase to check if there is already one here.

But I am using normal Fragment in Android. Hope it can help you a little.

Jeff Lee
  • 783
  • 9
  • 17