Communicative Between Fragments on Android

后端 未结 5 1581
日久生厌
日久生厌 2021-01-21 19:21

I am attempting to build an Android application that has two tabs, one for a textField/button and TreeMenu (where each element has a checkbox associated with it) and another for

5条回答
  •  梦谈多话
    2021-01-21 19:38

    In some of the answers there's a suggestion to put the ArrayList in a singleton, but that doesn't really solve your problem does it? Sure, you'll be able to access it from where ever you like, but it doesn't help you keeping two different fragments, both using the arraylist, in sync.

    Start by putting the ArrayList in a singleton as suggested or in your MainActivity. Once that is done you have at least two options for keeping the fragments in sync with the content of the ArrayList:

    1. Make the ArrayList Observable (check ObservableArrayList) and let the Fragments Observe the ArrayList for changes.

    2. Make use of an event bus (like Otto) and let the singleton or MainActivity (depending on where you put the ArrayList) post update events on the bus when the arraylist changes and have the Fragments subscribe to the events.

提交回复
热议问题