1

I have a fragment with ActionBar.TabListener. Inside the TabListener I call to show a fragment if the fragment has already been created. This works on portrait for all fragments, and on landscape for all fragments except one that contains a pagerViewer. For this problematic fragment, when the phone rotates (and re-select the tab), nothing displays.

Here's how I'm creating the fragments (condensed):

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.support.v4.app.FragmentActivity;
public class MainFragmentActivity extends FragmentActivity implements ResultsListener
{   
    FragmentManager fragment_manager;

@Override
protected void onCreate(Bundle savedInstanceState)
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_fragment);

    bundle = savedInstanceState;
    System.out.println("bundle = " + bundle);

    fragment_manager = getFragmentManager();

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setTitle(getApplicationContext().getApplicationInfo().labelRes);  

    ActionBar.Tab tab_jobList = actionBar.newTab().setText(R.string.stringJobs);
    tab_jobList.setIcon(R.drawable.text_list_white);

    actionBar.addTab(tab_jobList.setTabListener(new MyTabsListener<JobListLV>(this, "jobList", JobListLV.class, getFragmentManager())));
    actionBar.addTab(tab_trends.setTabListener(new MyTabsListener<TrendsPagerHolder>(this, "trendsPagerHolder", TrendsPagerHolder.class, getFragmentManager())));

}

Here's the inner class that has the TabListener:

    public MyTabsListener(Activity activity, String tag, Class<T> clz, Bundle args, FragmentManager fragmentManager) 
    {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;
        fm = fragmentManager;

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        Log.e("mFragment constructor", "mFragment = " + mFragment);

        fragmentTrendsPagerHolder = (TrendsPagerHolder) mActivity.getFragmentManager().findFragmentByTag("trendsPagerHolder");

        if (mFragment != null && !mFragment.isDetached()) 
        {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();

            if (fragmentTrendsPagerHolder != mFragment)         
            {
                ft.hide(mFragment);
                ft.commit();
            }
        }
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) 
    {
        displayTab(tab, ft);    
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) 
    {
        if (mFragment != null) 
        {
            ft.hide(mFragment);
        }           
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) 
    {
        displayTab(tab, ft);
    }

    private void displayTab(Tab tab, FragmentTransaction ft)
    {   
        fragmentJobListDetails = mActivity.getFragmentManager().findFragmentByTag("jobDetails");

        if (fragmentJobListDetails != null && !fragmentJobListDetails.isDetached()) 
        {
            ft.hide(fragmentJobListDetails);
        }

        if (mFragment == null || !mFragment.isAdded()) 
        {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } 
        else 
        {       
            System.out.println("mFragment.isAdded() = " + mFragment.isAdded());
            Log.e("mFragment", "mFragment = " + mFragment);
            ft.show(mFragment);
        }   
    }
}// end MyTabsListener

Any suggestions to solve this issue? Thanks.

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • Upon rotation, are your views re-created like they are supposed to be (unless otherwise specified in the AndroidManifest)? – shkschneider Dec 04 '14 at 16:58
  • I'm not sure what you're referring to specifically. My assumption was that the OS would retain the fragments by itself so I would just need to call them. Also, I don't think I've done anything special in the Manifest for this. – TooManyEduardos Dec 04 '14 at 17:33
  • Upon rotation, your `Activity` (by default) gets re-created. I'm not sure if it's related to your problem ; just a lead. – shkschneider Dec 04 '14 at 17:37
  • Yeah I know that Activities need to save their data on the Bundle, but my understanding is that Fragments get retained by the OS. When I run logs and get the memory location of the fragment, the data is exactly the same between the before version and the after rotation version – TooManyEduardos Dec 04 '14 at 17:38

1 Answers1

1

Call ft.commit() after ft.show(). So it should be like this:

private void displayTab(Tab tab)
{   
    FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();

    fragmentJobListDetails = mActivity.getFragmentManager().findFragmentByTag("jobDetails");

    if (fragmentJobListDetails != null && !fragmentJobListDetails.isDetached()) 
    {
        ft.hide(fragmentJobListDetails);
    }

    if (mFragment == null || !mFragment.isAdded()) 
    {
        mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
        ft.add(android.R.id.content, mFragment, mTag);
    } 
    else 
    {       
        System.out.println("mFragment.isAdded() = " + mFragment.isAdded());
        Log.e("mFragment", "mFragment = " + mFragment);
        ft.show(mFragment);
    }   

    ft.commit();
}

}// end MyTabsListener

Also, create a new fragment transaction to handle the possible transactions for the displayTab method.

Droid Chris
  • 3,455
  • 28
  • 31
  • This doesn't work. I get a runtime error of java.lang.IllegalStateException: commit already called – TooManyEduardos Dec 04 '14 at 17:34
  • Did you try my most recent modifications? – Droid Chris Dec 04 '14 at 19:52
  • Sorry I didnt notice there was a new modification. I tried the entire method and the commit doesn't give me a runtime error anymore, but I go back to not displaying anything after rotation. Could this be an issue with my onCreate method or the fact that I'm not using the support libraries of Fragment? – TooManyEduardos Dec 04 '14 at 19:58
  • Do you have any other suggestions? Thanks for your help – TooManyEduardos Dec 04 '14 at 21:34
  • This should help you with getting the fragment instances after rotation. http://stackoverflow.com/questions/5164126/retain-the-fragment-object-while-rotating – Droid Chris Dec 04 '14 at 22:38
  • I have tried this way before, but I haven't been able to instantiate the fragments this way. My current way is by adding them directly into the actionBar.tab. The issue is that the actionbar is empty after rotation if I do it the way suggestion on that question – TooManyEduardos Dec 04 '14 at 22:41