Communicating between a fragment and an activity - best practices

后端 未结 9 1317
予麋鹿
予麋鹿 2020-11-22 02:45

This question is mostly to solicit opinions on the best way to handle my app. I have three fragments being handled by one activity. Fragment A has one clickable element th

相关标签:
9条回答
  • 2020-11-22 03:15

    To communicate between an Activity and Fragments, there are several options, but after lots of reading and many experiences, I found out that it could be resumed this way:

    • Activity wants to communicate with child Fragment => Simply write public methods in your Fragment class, and let the Activity call them
    • Fragment wants to communicate with the parent Activity => This requires a bit more of work, as the official Android link https://developer.android.com/training/basics/fragments/communicating suggests, it would be a great idea to define an interface that will be implemented by the Activity, and which will establish a contract for any Activity that wants to communicate with that Fragment. For example, if you have FragmentA, which wants to communicate with any activity that includes it, then define the FragmentAInterface which will define what method can the FragmentA call for the activities that decide to use it.
    • A Fragment wants to communicate with other Fragment => This is the case where you get the most 'complicated' situation. Since you could potentially need to pass data from FragmentA to FragmentB and viceversa, that could lead us to defining 2 interfaces, FragmentAInterface which will be implemented by FragmentB and FragmentAInterface which will be implemented by FragmentA. That will start making things messy. And imagine if you have a few more Fragments on place, and even the parent activity wants to communicate with them. Well, this case is a perfect moment to establish a shared ViewModel for the activity and it's fragments. More info here https://developer.android.com/topic/libraries/architecture/viewmodel . Basically, you need to define a SharedViewModel class, that has all the data you want to share between the activity and the fragments that will be in need of communicating data among them.

    The ViewModel case, makes things pretty simpler at the end, since you don't have to add extra logic that makes things dirty in the code and messy. Plus it will allow you to separate the gathering (through calls to an SQLite Database or an API) of data from the Controller (activities and fragments).

    0 讨论(0)
  • 2020-11-22 03:21

    I'm not sure I really understood what you want to do, but the suggested way to communicate between fragments is to use callbacks with the Activity, never directly between fragments. See here http://developer.android.com/training/basics/fragments/communicating.html

    0 讨论(0)
  • 2020-11-22 03:22

    The easiest way to communicate between your activity and fragments is using interfaces. The idea is basically to define an interface inside a given fragment A and let the activity implement that interface.

    Once it has implemented that interface, you could do anything you want in the method it overrides.

    The other important part of the interface is that you have to call the abstract method from your fragment and remember to cast it to your activity. It should catch a ClassCastException if not done correctly.

    There is a good tutorial on Simple Developer Blog on how to do exactly this kind of thing.

    I hope this was helpful to you!

    0 讨论(0)
  • 2020-11-22 03:30

    I am using Intents to communicate actions back to the main activity. The main activity is listening to these by overriding onNewIntent(Intent intent). The main activity translates these actions to the corresponding fragments for example.

    So you can do something like this:

    public class MainActivity extends Activity  {
    
        public static final String INTENT_ACTION_SHOW_FOO = "show_foo";
        public static final String INTENT_ACTION_SHOW_BAR = "show_bar";
    
    
       @Override
       protected void onNewIntent(Intent intent) {
            routeIntent(intent);
       }
    
      private void routeIntent(Intent intent) {
           String action = intent.getAction();
           if (action != null) {               
                switch (action) {
                case INTENT_ACTION_SHOW_FOO:
                    // for example show the corresponding fragment
                    loadFragment(FooFragment);
                    break;
                case INTENT_ACTION_SHOW_BAR:
                    loadFragment(BarFragment);
                    break;               
            }
        }
    }
    

    Then inside any fragment to show the foo fragment:

    Intent intent = new Intent(context, MainActivity.class);
    intent.setAction(INTENT_ACTION_SHOW_FOO);
    // Prevent activity to be re-instantiated if it is already running.
    // Instead, the onNewEvent() is triggered
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    getContext().startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 03:32

    The suggested method for communicating between fragments is to use callbacks\listeners that are managed by your main Activity.

    I think the code on this page is pretty clear: http://developer.android.com/training/basics/fragments/communicating.html

    You can also reference the IO 2012 Schedule app, which is designed to be a de-facto reference app. It can be found here: http://code.google.com/p/iosched/

    Also, here is a SO question with good info: How to pass data between fragments

    0 讨论(0)
  • 2020-11-22 03:35

    It is implemented by a Callback interface:

    First of all, we have to make an interface:

    public interface UpdateFrag {
         void updatefrag();
    }
    

    In the Activity do the following code:

    UpdateFrag updatfrag ;
    
    public void updateApi(UpdateFrag listener) {
            updatfrag = listener;
    }
    

    from the event from where the callback has to fire in the Activity:

    updatfrag.updatefrag();
    

    In the Fragment implement the interface in CreateView do the following code:

     ((Home)getActivity()).updateApi(new UpdateFrag() {
            @Override
            public void updatefrag() {
                  .....your stuff......
            }
     });
    
    0 讨论(0)
提交回复
热议问题