I have two fragments. The checkNodesFragment creates dynamically an ArrayList of checkBoxes and then the ReserveNodesFragment should access which of them were checked in checkNodesFragment. What storage method is better to use, so as ReserveNodesFragment can see what checkBoxes where checked?
After searching, i found that SharedPreferences is a method to store data permanently. Is SharedPreferences suitable for making data visible through fragments? I am newbie in android, so sorry if the question is obvious.
The communication between fragments should be done via Activity
.
Activity
to Fragment
Create a bundle and use fragment.setArguments(bundle)
to pass data to Fragment
.
Fragment
to Activity
create an interface
in your fragment
and in your Activity
implement the interface
More info:
Communicating with Other Fragments (Download the sample).
If you wish to transfer data across fragments in runtime you can simply use Activity.
Every fragment has method getActivity()
. It's a kind of parent for your fragments so you can use it as a "bridge". Nice solution would be defining callbacks in your fragments. Then your Activity could be working as registered observer for these callbacks and react accordingly e.g. listen for changes in the first fragment and then set something on the second fragment.
In more details.
- Define
interface
in your fragment that provides information about changes. Create method that will be called in fragment when checkbox is selected. - In
onAttach()
method check ifgetActivity()
is instance of thatinterface
- Make you Activity to implement that
interface
- Implement method in Activity. Inside that method call second fragment and pass updated information.
Nice example can be found when you create an Activity from a template with fragment in Android Studio.
As i mentioned, i have two fragments. checkNodesFragment creates dynamically an ArrayList of checkBoxes and then the ReserveNodesFragment should access which of them were checked in checkNodesFragment . But i have six Arraylists of checkBoxes that ReserveNodesFragment should know:
//The ArrayLists with the checkBoxes
public ArrayList<CheckBox> orbitCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> gridCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> usrpCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> disklessCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> icarusCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> baseStationsCheckBoxes = new ArrayList<CheckBox>();
So according to your suggestion i should pass six parameters to the method of the interface, so as ReserveNodesFragment will have access to the six ArrayLists:
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onReserveSelected(ArrayList<CheckBox> orbitCheckBoxes, ArrayList<CheckBox> gridCheckBoxes, ArrayList<CheckBox> usrpCheckBoxes, ArrayList<CheckBox> disklessCheckBoxes,
ArrayList<CheckBox> icarusCheckBoxes, ArrayList<CheckBox> baseStationsCheckBoxes);
}
So it is elegant to pass so many parameters to the callback method?
Also, the checkNodesFragment is a nested fragment to the AvailableNodesFragment. So its parent is not an activity but a Fragment.
So the app hierarchy is like this:
NitosSchedulerActivity --> AvailableNodesFragment-->CheckAvailableNodesFragment
NitosSchedulerActivity --> ReserveNodesFragment
The CheckAvailableNodesFragment is a nested Fragment to AvailableNodesFragment.
The ReserveNodesFragment is triggered by a Button of the AvailableNodesFragment. So, there is not a way to trigger a callback method from CheckAvailableNodesFragment to ReserveNodesFragment for passing the ArrayLists.
So, until now the only way i found to make the ArrayLists of CheckBoxes visible to the ReserveNodesFragment is to make the ArrayLists declared globally.
However, making ArrayLists global is an elegant solution?
来源:https://stackoverflow.com/questions/25062879/sharing-data-between-android-fragments