getSupportFragmentManager doesn't compile on DialogFragment
Asked Answered
L

2

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()...

Lunar answered 14/12, 2013 at 21:27 Comment(4)
What do you mean by not resolved ? Do you have any stack trace ?Bull
@Bull it does not compile. You know red sqiggly line.Lunar
lol. Where ? Which line, highlight //the culprit lineBull
The one with getSupportFragmentManager on as I mentioned.Lunar
B
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).

Bull answered 14/12, 2013 at 21:37 Comment(2)
So use Fragments & FragmentManager from the support library as well.Bull
Problem solved. The mismatch was solved by making my DialogFragment also come from the support lib.Lunar
S
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()

Shovelhead answered 14/12, 2013 at 21:42 Comment(5)
no getFragmentManager return the one from the activity. See the javadoc.Bull
Then you need to make sure that both your Fragment and DialogFragment classes are from the v4 support library. (and FragmentManager)Shovelhead
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.Shovelhead
Really, the docs doesn't say so : developer.android.com/reference/android/support/v4/app/…Bull
There was a reason for not using that method, but I can't for the life of me remember why.Shovelhead

© 2022 - 2024 — McMap. All rights reserved.