6

How can I show this :

public class TagsDialog extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.tags_dialog, null));
        builder.setMessage("This is a message").setTitle("TAGS");
        return builder.create();
    }
}

From inside my Fragment inside a ViewPager :

    public class MyFragment extends Fragment
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

    ...

            ImageView btnTags = (ImageView)view.findViewById(R.id.btnTags);
            btnTags.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {

                    DialogFragment dlg = new TagsDialog();
                    //this line doesn't compile
                    dlg.show(getSupportFragmentManager(), "tags");

                }
            });
}
}

I have tried for ages to get this to work, but getSupportFragmentManager is never resolved... any ideas?

EDIT:

I feel this is all caused by the support FragmentManager vs the android.app.FragmentManager, however I do not know how to solve this, as I am using the ViewPager from the support library...

getSupportFragmentManager/and all related getFragManager methods like parent and child one always returns the Manager from the support lib, wheras the show method wants the core one.

Imports are:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

FragmentManager fm = getActivity().getSupportFragmentManager(); // returns from support lib
DialogFragment dlg = new TagsDialog();
dlg.show(fm, "tags"); // wants core...

If I just use core, then getSupportFragmentManager() does not exist on getActivity()...

Snicolas
  • 37,840
  • 15
  • 114
  • 173
sprocket12
  • 5,368
  • 18
  • 64
  • 133

2 Answers2

14

You should double check your imports. You can't use a mix of Fragment & FragmentManager from the support library and from android core apis.

If you use support, use everything from support (from package android.support.v4.app). If not, use everything from core api package (android.app).

Snicolas
  • 37,840
  • 15
  • 114
  • 173
4

Here is what I do in my Fragment that is in a ViewPager. The code to show a DialogFragment is the same where ever you would do it though.

FragmentManager fm = getActivity().getSupportFragmentManager();
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog();
dialog.setTargetFragment(RecipeFragment.this, REQUEST_CONFIRM_RC);
Bundle b = new Bundle();
b.putString(ConfirmDeleteDialog.EXTRA_CONFIRM_DIALOG_TYPE,
        ConfirmDeleteDialog.CONFIRM_DIALOG_TYPE.COMMIT.name());  // enum determines what to display
dialog.setArguments(b);
dialog.show(fm, "confirm delete");

EDIT.

I think you may be missing the *getActivity().*getSupportFragmentManager()

Rick Falck
  • 1,778
  • 3
  • 15
  • 19
  • no getFragmentManager return the one from the activity. See the javadoc. – Snicolas Dec 14 '13 at 21:50
  • Then you need to make sure that both your Fragment and DialogFragment classes are from the v4 support library. (and FragmentManager) – Rick Falck Dec 14 '13 at 21:53
  • You may be right (that he has imported the wrong classes), but the support versions need the getActivity() too when done from a support Fragment. – Rick Falck Dec 14 '13 at 21:54
  • Really, the docs doesn't say so : http://developer.android.com/reference/android/support/v4/app/Fragment.html#getFragmentManager() – Snicolas Dec 15 '13 at 07:14
  • There was a reason for not using that method, but I can't for the life of me remember why. – Rick Falck Dec 15 '13 at 08:47