0

a quick question about managing fragments. Right now I am using this structure:

I have one MainActivity and multiple Fragments i.e. LoginFragment, InventoryFragment, NetworkFragment etc. I am using the FragmentManager for transcations and it works greatly so far. But I have run into one issue. It is about handling the callbacks from the Fragments to the Activity.

I am using the simple way with interface callbacks which I define in the fragments and implement in the MainActivity, but as I get more and more Fragments it is getting really overcrowded with interfaces.

It looks for example like this

public class MainActivity extends AppCompatActivity implements LoginFragmentCallback, InventoryFragmentCallback, NetworkFragmentCallback, AnyFragmentCallback

So my question is how can I manage the communication between Fragment and Activity better?

Thx for any help!


EDIT 1: I got an idea how to solve this with the comment from user @Rohit5k2

In my MainActivity I made a public method

public class MainActivity extends AppCompat{
  public callMe();
} 

In my Fragment I can call this method via

public class AnyFragment extends Fragment{
  private void someMethod(){
    ((MainActivity)getActivity()).callMe();
}

Seems to solve the problem I think. Or is it bad? With this I dont need to implement any callback since I have a reference to the MainActivity with Fragments.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • I think that is the best option you've got. It's better than holding on to references to your fragments inside your activity. – Mohammed Aouf Zouag Feb 01 '16 at 12:36
  • Why don't you implement the interface in fragments only? Implement only which is required. – Rohit5k2 Feb 01 '16 at 12:36
  • 1
    Use local broadcasts. All fragments should broadcast events to the main activity. The main activity can update UI upon receiving events or decide what action to perform next. http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – ihsan Feb 01 '16 at 12:41
  • Well, I guess this is a question with a lot of possible answers. I tend to keep my activities as slim as possible and nest related fragments instead. Talking to the parent fragment either using callback, or setTargetFrament – cYrixmorten Feb 01 '16 at 12:48
  • Regarding your edit, I would suggest that you ensure that the activity is instanceof MainActivity in the onAttatch method of your fragments. Or even better, have the activity implement a callback interface and check for that the same way. This way you are allowed to add other activities later on, while still enabling reuse of your fragments – cYrixmorten Feb 01 '16 at 12:54
  • @cYrixmorten Thanks for the tip. I was not planning to use more than one Activity. I think I will have to look into the pro's of using multiple Activities over Fragments to decide my further approach. – Murat Karagöz Feb 01 '16 at 12:59
  • Check this link with detail description and examples http://stackoverflow.com/a/35013382/2826147 – Amit Vaghela Feb 01 '16 at 13:01

3 Answers3

2

I like the activity + lot of fragments myself, so I'm using this approach.

Activity implements interface

 interface ControllerAccess {
    <T> getController(Class<T> clazz);
 }

I have a Map<Class,Object> in activity. In onCreate i populate it with fragment specific controller class like yours LoginFragmentCallback etc.

map.put(LoginFragmentCallback.class,new LoginFragmentCallbackImpl());

and interface implementation looks like this

public <T> T getController(Class<T> clazz) {
   return (T) map.get(clazz);
 } 

And in fragment i make something like

LoginFragmentCallback callback;

void onAttach(Activity activity) {

    if (activity instanceof ControllerAccess) {
        callback = (ControllerAccess)activity.getController(LoginFragmentCallback.class);  
    }
}
aelimill
  • 1,015
  • 1
  • 10
  • 17
0

If you need to send data from the Fragment to the Activity, you can use getActivity and cast it to your main activity.

If you havr to go the other direction, maybe you are doing something wrong. A Fragment has to be independent enough from it's hosting activity.

Another suggestion is using an Activity for the login and after that start your main one.

For other kinds of communication, I recommend EventBus: https://github.com/greenrobot/EventBus

gilgil28
  • 540
  • 5
  • 12
  • Thanks for the suggestions. Seems like this is a valid solution. I am in the mindset of using one single Activity and split up the work into fragments. I will accept this later if there is no other suggestion. – Murat Karagöz Feb 01 '16 at 12:53
0
public class MainActivity extends FragmentActivity implements FragmentInteractionListener {

 //your main activity code
}

public class Fragment1 extends Fragment {

//you can get resource from MainActivity using below code
(MainActivity) getActivity().getSomeInfo())

}
Sarav
  • 17
  • 2
  • 11