Murat Karagöz
Murat Karagöz

Reputation: 37584

How to manage multiple Fragments with one single Activity?

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.

Upvotes: 0

Views: 2693

Answers (3)

aelimill
aelimill

Reputation: 1015

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);  
    }
}

Upvotes: 2

Sarav
Sarav

Reputation: 17

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

}

Upvotes: 0

gilgil28
gilgil28

Reputation: 540

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

Upvotes: 0

Related Questions