Communication between Fragments in ViewPager

前端 未结 4 1035
天命终不由人
天命终不由人 2020-12-01 06:57

I\'m trying to do this: http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html Except that I\'m using a FragmentStatePagerAdapter

I have an

相关标签:
4条回答
  • 2020-12-01 07:32

    Fragments have access to there parent Activity.
    Therefore the simplest approach is to register a callback in the parent Activity.

    Update: Submit cache added to MainActivity.

        public class MainActivity extends FragmentActivity {
    
            private OnButtonClicked mOnButtonClicked;
            private String mSubmitCache;
    
            public interface OnButtonClicked {
                void submit(final String s);
            }
    
            public void setOnButtonClicked(final OnButtonClicked c) {
                mOnButtonClicked = c;
                // deliver cached string, if any
                if (TextUtils.isEmpty(mSubmitCache) == false) {
                    c.submit(mSubmitCache);
                }
            }
    
            public void buttonClicked(final String s)  {
                if (mOnButtonClicked == null) {
                    // if FragmentB doesn't exist jet, cache value
                    mSubmitCache = s;
                    return;
                }
                mOnButtonClicked.submit(s);
            }
        }
    
        public class FragmentA extends Fragment implements OnClickListener {
    
            private MainActivity mMain;
            private Button mButton;
    
            @Override public onAttach(Activity a) {
                mMain = (MainActivity) a;
            }
    
            @Override public void onClick(View v) {
                mMain.buttonClicked("send this to FragmentB.");
            }
        }
    
        public class FragmentB extends Fragment implements MainActivity.OnButtonClicked {
    
            private MainActivity mMain;
            private TextView mTextView;
    
            // Called when the fragment's activity has been created
            // and this fragment's view hierarchy instantiated
            @Override public void onActivityCreated(Bundle savedState) {
                mMain = (MainActivity) getActivity();
                mMain.setOnButtonClicked(this);
            }
    
            @Override void submit(final String s) {
                mTextView.setText(s);
            }
        }
    
    0 讨论(0)
  • 2020-12-01 07:35

    FragmentB is not even created until you switch to it so fragmentB.updateText(textPassToB); gives you NullPointerException.

    You will need to store the text from the EditText in your activity and later when (if) the FragmentB is created you will need to read value from it.

    0 讨论(0)
  • 2020-12-01 07:37

    I use Mr. Rodion's solution above. But in addition, Android Studio asked me to add @Subscribe annotation before onEvent method.

    Like this:

    @Subscribe
    public void onEvent(TextChangedEvent event) {
        textView.setText(event.newText);
    }
    

    According to EventBus’ API:

    Subscribers implement event handling methods (also called “subscriber methods”) that will be called when an event is posted. These are defined with the @Subscribe annotation. Please note that with EventBus 3 the method name can be chosen freely (no naming conventions like in EventBus 2).

    0 讨论(0)
  • 2020-12-01 07:46
    1. You could use Intents (register broadcast receiver in fragment B and send broadcasts from fragment A.
    2. Use EventBus: https://github.com/greenrobot/EventBus. It's my favorite approach. Very convinient to use, easy communications between any components (Activity & Services, for example).

    Steps to do:

    First, create some class to represent event when your text changes:

    public class TextChangedEvent {
      public String newText;
      public TextChangedEvent(String newText) {
          this.newText = newText;
      }
    }
    

    Then, in fragment A:

    //when text changes
    EventBus bus = EventBus.getDefault();
    bus.post(new TextChangedEvent(newText));
    

    in fragment B:

    EventBus bus = EventBus.getDefault();
    
    //Register to EventBus
    @Override
    public void onCreate(SavedInstanceState savedState) {
     bus.register(this);
    }
    
    //catch Event from fragment A
    public void onEvent(TextChangedEvent event) {
     yourTextView.setText(event.newText);
    }
    
    0 讨论(0)
提交回复
热议问题