Steve
Steve

Reputation: 11

FragmentManager has not been attached to a host because it was null after calling getSupportFragmentManager

I am working on an App that will have a single activity and multiple fragments.

The flow will be:

MainActivity with a fragment container.

I am currently just playing with code while learning about fragments and navigation while researching answers for my questions and examples of code.

My test app has my MainActivity and my initial fragment working. When I try to handle the clicked textview and display a new fragment I get this exception.

java.lang.IllegalStateException: FragmentManager has not been attached to a host.
at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1880)
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:329)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:294)
at com.gallery.booklibrary.MainActivity.setupByAuthorFragment(MainActivity.java:69)
at com.gallery.booklibrary.MainFragment$1.onClick(MainFragment.java:75)

In my MainActivity.java file, I do this in OnCreate.

String lTag = "Main";
MainFragment lFragment = new MainFragment();
FragmentManager lMyFm = this.getSupportFragmentManager();
FragmentTransaction lMyFt = lMyFm.beginTransaction();
lMyFt.add(R.id.fragmentContainerView, lFragment, lTag);
lMyFt.addToBackStack(lTag);
lMyFt.commit();

In my MainFragment.java this is my setOnClickListener code:

        TvByAutor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: TvByAuthor");
                new MainActivity().setupByAuthorFragment();                 // MainFragment.java:75
            }
        });

This onClick calls this method in my MainActivity.java file.

    public void setupByAuthorFragment(){
        Log.d(TAG, "setupByAuthorFragment: ");
        String lTag = "ByAuthor";

        ByAuthorFragment lFragment = new ByAuthorFragment();
        FragmentManager lMyFm = this.getSupportFragmentManager();       // MainActivity.java:64
        FragmentTransaction lMyFt = lMyFm.beginTransaction();

        lMyFt.add(R.id.fragmentContainerView, lFragment, lTag);
        lMyFt.addToBackStack(lTag);
        lMyFt.commit();                                                // MainActivity.java:69
    }

Thru debugging I have determined that MainActivity.java:64 is returning a null for the getSupportFragmentManager call.

I thought I was following 'best practices' by doing the fragment switching in the MainActivity, but my testing tells me this doesn't work. If I do the work in the MainFragment.java file, it works when I use 'getActivity().getSupportFragmentManager();'.

I do not understand why the getSupportFragmentmanager call does not work in the MainActivity.java file.

Any assistance would be appreciated. Thanks

Lots and Lots of research. Many threads say what I am doing should work, but I haven't had an7 luck getting the code to work.

Upvotes: 0

Views: 419

Answers (1)

Steve
Steve

Reputation: 11

Based on information found on another web site, I determined I was incorrect in how I was calling the method in my activity. I changed my click listener code in my MainFragment.java class for my textview to this and my exception no longer occurs.

TvByAuthor.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: TvByAuthor");
        ((MainActivity)getActivity()).setupByAuthorFragment();
    }
});

Upvotes: 0

Related Questions