0

I have got "Main" fragment with two nested fragment.

I setTargetFragmet for nested fragments trailMapFragment.setTargetFragment(SORestaurantsMapFragment.this, 0);

And I am using this interface in nested fragments.

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     onAttachFragment(getTargetFragment());
     this.mPagerAdapter = new SORestaurantsPagerAdapter(getChildFragmentManager(), this.sourceInterfaceMP.restaurants());
}

public void onAttachFragment(Fragment fragment) {

    try  {
            this.iTrailInteractionInterfaceMP = (ISOMapInterface.IInteractionInterfaceMP) fragment;
            this.sourceInterfaceMP = (ISOMapInterface.ISourceInterfaceMP) fragment;

        } catch (ClassCastException e) {
            throw new ClassCastException(fragment.toString() + " must implement ITrailContentInterface");
        }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    setTargetFragment(null, -1);
    super.onSaveInstanceState(outState);
}

I have got setTargetFragment(null, -1), because I had any exception and I found this solution on stackoverflow.

I need interface in fragment (not in activity).

Finally, I have got exception in onCreate:

Attempt to invoke interface method 'java.util.List pl.controller.map.ISOMapInterface$ISourceInterfaceMP.restaurants()' on a null object reference
       at pl.controller.map.SOMapPagerFragment.onCreate(SOMapPagerFragment.java:49)

What is the best solution for poblem? Any idea?

I think about setDelegate(Interface) method in nested fragments (when fragment is create and before attach with fragmentManager), but I think it doesn't work when fragment is reCreated.

Unmerciful
  • 1,325
  • 4
  • 21
  • 38

0 Answers0