The onResume fragment is not called after the fragment detaches and then reattaches

I am trying to get a handle to all the new ActionBar and Fragments APIs. I have a main action, and I want it to manage two different tabs. I use ActionBarSherlock to support an older version than ICS.

Each tab contains its own Fragment(each one is a subclass SherlockListFragment) I got it to work mostly well, but I have a problem that I'm sure this is stupid, but I still can not figure it out:

For the first time, each fragment is displayed, everything is in order, the list is filled, and therefore MenuItems are in the ActionBar.

But the second time you see a tab (after swicth and switch-back), neither the list is populated, nor the ActionBar MenuItems.

This is how I switch 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 separate the fragment:

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

I populate ActionBar lists and menus in onResume:

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

I also tried it on onStart and onCreateView, but that did not help ...

So what am I missing here? And if there are other problems in my code that I don’t know about, please tell me.

Thank!

EDIT:

I just confirmed that onResumedosen't will be called after I switch tabs, which is definitely wrong, since I disconnect and reconnect them ... Do I switch tabs incorrectly?

+5
source share
2 answers

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

+2
source

All Articles