0

I've got an Application with a MainActivity with a Navigation Drawer Menù.

Inside the MainActivity View I've got a Frame layout which takes the whole space.

When the user select something from the menù I call a method which handle the fragments transaction inside the Frame Layout.

My MainActivity is the Fragment manager and from here i can handle all the changes I want and i can handle the communications between the fragments.

Here's the problem:

In one of my Fragment i'd like to show 2 new fragments, one with a PieChart and one with the data.

I could have done only one Fragment with all inside its view but i'd like to use a modular managing way.

So, first of all i created 1 fragment, the Container one (we can call it A).

In its view i put the fragment B and fragment C, directly from Xml.

In fragment B i've got the PieChart, HERE i call the methods to download data from a database, and then i put this data inside the chart.

I wrote a simple Interface which contains a method which is called when the user interact wich the chart; it build an object whith the selected data.

My target is to send this data to Fragment A and then pass them to Fragment C, so that the user can see what is he selecting.

In Fragment A i've Implemented the Fragment B inteface, then i set up this code inside OnAttach of B:

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        Interface = (InterfaceB) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement InterfaceB");
    }
}

Everithing seems to works, but in the moment tha Fragment A is created, OnAttach of B is called, and the context which arrives refers to MainActivity and not to Fragment A.

This causes a Crash.

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47

1 Answers1

1

Fragment is not an instance of Context, so it is impossible to pass it to onAttach(Context Context) anyway.

There are two ways how you can do what you want.

1) Use onAttachFragment(Fragment fragment) inside fragment A, and catch events when fragments B and C are attached;

2) Use getParentFragment() inside fragments B and C, to access A.

Ufkoku
  • 2,384
  • 20
  • 44
  • Do you know how can I identify which one of the two fragment is getting attacched when onAttachFragment is called? Thanks again – L. Gangemi Mar 20 '17 at 11:05
  • Sometimes, i can't figure exacly when, if i call getParentFragment() inside fragment B and C during onAttach, it returns null, and i can't connect my interface – L. Gangemi Mar 23 '17 at 14:34
  • If fragment attached straight to activity `getParentFragment()` returns null, to attach B and C to A, you must use `getChildFragmentManager()` for your transactions inside A instead of `getSupportFragmentManager()`. Also you must not define nested fragments inside xml – Ufkoku Mar 23 '17 at 14:50
  • Thanks for your fast answer. Actually i'm using child fragment manager to perform the change. – L. Gangemi Mar 23 '17 at 14:52
  • @L.Gangemi in OP you wrote "In its view i put the fragment B and fragment C, directly from Xml." Transactions from xml use `getSupportFragmentManager()` – Ufkoku Mar 23 '17 at 14:55
  • You're right. I just get the error and it wasn't caused by the transaction. Thanks a lot anyway for the help ;) – L. Gangemi Mar 23 '17 at 15:01