In my App i start with TabbedView that have MainActivity with container that show 3 Fragments for each time (depend what user select).
The MainActivity.java start a Bluetooth comunication with OBD car and request commands until the app in on. Now, when the OBD respond in my MainActivity i had an Handler that take the response and with setter() method put this method inside a class PidsCommand.java.
After that i would take this Object that contain all updated data to the Fragment that show that data in real time. So i need to pass this object when the DashboardFragment is created and showed...
There is some best way to do that? I'm in the right way? And how can i share that Object created in the MainActivity to the Fragment so i can take data with getter() method and popolate the view with that data?
EDIT
As i can see i only need to pass the object when i create a newInstace of Fragment... don't need the interface because i need to send the object when i create the Fragment for read it and show data.
This is the istance of Framgent:
public static DashboardFragment newInstance(OBDPids pids) {
DashboardFragment dashboardfragment = new DashboardFragment();
Bundle argsPids = new Bundle();
argsPids.putBundle("pids", pids);
dashboardfragment.setArguments(argsPids);
return dashboardfragment;
}
But putBundle and putParcable are good good.... how can i pass my obect with Instance?
And in the Fragment i take data back with:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
Bundle args = getArguments();
OBDPids pids = args.getParcelable("pids");
return view;
}
Or is not a good way for do that?
EDIT 2
This solution can be good instead of interface?
In the MainActivity.java
pids = new OBDPids();
public OBDPids getMyObject() {
return pids;
}
In the Fragment
OBDPids myObjectFromActivity = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
MainActivity activity = (MainActivity) getActivity();
myObjectFromActivity = activity.getMyObject();
return view;
}