3

I'm going crazy trying to get an optionsMenu to have different options for different views. I could have it working if onResume() was called on my fragments, but it isn't.

I have one SherlockFragmentActivity which during onCreate adds a SherlockFragment like this:

if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            Fragment f = LoginFragment.newInstance();

            getSupportFragmentManager()
                    .beginTransaction()
                    .add(android.R.id.content, f, "loginfragment")
                    .attach(f)
                    .commit();
        }

This eventually adds another SherlockFragment on top of it using the same method:

Fragment f = OfferListFragment.newInstance();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, f, "offerList")
                .addToBackStack(f.getClass().getSimpleName())
                .commit();

and finally this launches yet another SherlockFragment on top using the method:

AddOfferFragment newFragment = AddOfferFragment.newInstance(offer);

        getActivity()
                .getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, newFragment, "addofferdialog")
                .addToBackStack(newFragment.getClass().getSimpleName())
                .commit();

I've added onResume() events like so:

@Override
    public void onResume() {
        Log.e(TAG, "onResume");
        super.onResume();
    }

now I see onResume logs when they are created, but when I pressBackfrom any of them, I don't get theonResume` log's at all. 1 of my options is to logout which dismisses all views like this:

FragmentManager fm = getSupportFragmentManager();

        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack(fm.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }

but I don't even get onResume on the very first fragment.

Due to another issue I have with the fragments being transparent here (This may be normal behaviour, i'm not sure). I'm wondering if the way I am adding the fragments is wrong?

If not, what else could be the problem?

Community
  • 1
  • 1
Darren
  • 10,182
  • 20
  • 95
  • 162
  • Not sure if this will change your situation, but (1) You do not need to call both `add()` and `attach()` in a `FragmentTransaction`. Just `add()` should be enough. (2) When you place another fragment "on top", do you mean you are trying to replace the exiting one with a new one? In that case, you should use `replace()` instead of `add()` in your `FragmentTransaction`. – Karakuri May 16 '13 at 18:16
  • Thanks for the comment. I shall ammend 1. 2, I show the next fragment full screen, I leave the original ones so the user can click back to go back to them. Is this not the right way to do it? – Darren May 16 '13 at 18:32
  • I had the same reply just now on my other question regarding the transparency and using .replace instead of .add fixed all my problems. Thanks – Darren May 16 '13 at 19:09
  • I am having the same issue as Darren onResume is not getting called when the fragment becomes visible. . – real 19 Sep 29 '14 at 06:14
  • Answer to Darren question included transparency and using .replace instead of .add http://stackoverflow.com/questions/16575177/fragment-is-transparent-and-shows-activity-below – JonasSeputis Jul 04 '16 at 12:53

1 Answers1

-1

As noted by JonasSeputis and the OP himself, the solution was to change the transaction.add() to transaction.replace()

The detailed solution is here.