I hope you guys can clarify something for me.
I have been using Android for about 6 months, and I'm still confused about the best way to communicate between Activity and Fragments. I already read the information on the Android developer site .
Now I know with absolute certainty 100% that the interface is the best way to communicate with Fragment to Activity. That is, creating an interface in your fragment and allowing your activity to implement it. This way you can call a method from your interface inside your fragment and process it using Activity (which implements the interface).
Here, I'm not sure. The Android developer site reports that you must place your objects in the Bundle to communicate with the fragment.
Bundle bundle = new Bundle(); bundle.putInt(SOME_IDENTIFIER, myInt);
Now I know how to use the Singleton class every time I have some functions that I can separate. Say I have a Singleton with a name PersistenceServicewhere I handle all things related to conservation, e.g. keeping something in SharedPreferences. It PersistenceServicewill then contain methods of type putMyString(String key, String myString)or putSomeObject(String key, SomeObject someObj). Thus, the class itself should not cope with persistence, but can simply call PersistenceServicefor this.
PersistenceService
SharedPreferences
putMyString(String key, String myString)
putSomeObject(String key, SomeObject someObj)
Now say that I need to update something in my fragment, TextView or something like that. This is what I do:
String myString = PersistenceService.getInstance(getActivity()).getMyString(someKey); textView.setText(myString);
I pass context ( getActivity()) because I need it to receive SharedPreferences.
getActivity()
Now my real question is:
Activity Bundle? .
- , , , , . , , .. .
, , , " !".
, , , "" "" - , , "Bundle" info Singleton, , , , , , , , , , .. :
Bundle, , - , , , , , , , , - ( ), ? , .
, , , , - , , , , , , , .
, !
!
, ( , ).
, - , - , , , .
Please understand that 2 fragments cannot directly communicate with each other, they need help in the action in the context of which they are created. Using the interface :). Embed interface in fragment
interface StartCommunication
StartCommunication
Define an interface in activity whose fragment uses
public class MainActivity extends Activity implements SendFragment.StartCommunication