Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment onResume doesn't get called after Fragment is being detached and then re-attached

I'm trying to get the handle on all the new ActionBar and Fragments API. I have an main activity, and I want it to manage two different tabs. I'm using the ActionBarSherlock in order to support older version than ICS.

Each tab contains its own Fragment (each one is a subclass of SherlockListFragment) I got it to work basically nice, but I have a problem that I'm sure that is stupid, but I can't figure it out yet:

On the first time each Fragment is shown, everything is OK, the list is populated and so the MenuItems in the ActionBar.

But the second time you see a tab (After swicth and switch-back), Neither the list get populated, nor the ActionBar MenuItems.

This is how I'm switching the tabs:

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
    SherlockListFragment toAttach = // Find the right fragment here...

    if (toAttach != null) {
        if (toAttach.isAdded() == false) {
            transaction.add(R.id.tab_placeholder, toAttach,
                    REMINDER_FRAGMENT_TAG);
        } else {
            transaction.attach(toAttach);
        }
    }
}

And onTabUneselect I'm detaching the Fragment:

@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
    SherlockListFragment toDetach = // Find the right fragment
    if (toDetach != null) {
        transaction.detach(toDetach);
    }
}

I'm populating the lists and the ActionBar menu in onResume:

@Override
public void onResume() {
    super.onResume();
    setHasOptionsMenu(true);
    fillRemindersList();
}

I also tried it in onStart and onCreateView but it didn't help...

So what am I missing here? And if there are others issues in my code that I'm unaware of, please do tell.

Thanks!

EDIT:

I just confirmed that onResume dosen't get called after I switch tabs, which is definetly wrong since I'm detaching and re-attaching them... Am I switching tabs the wrong way?

like image 861
Avi Shukron Avatar asked Nov 04 '22 01:11

Avi Shukron


1 Answers

Try using transaction.remove(fragment) in onTabUnselected and transaction.replace in onTabSelected.

like image 56
Sid Patel Avatar answered Nov 12 '22 13:11

Sid Patel