What is the best way to access data members of an activity from its fragment
Some ways which I know include -
Actually the combination of the first and third method is the best. The second one should be avoided at all cost since this strongly couples the Fragment to a specific
Activity. This will defeat one of the main advantages of
Fragments namely being able to use it in different
Activities (plug and play).
As for the first and third method.
- The first one is how you will usually communicate from the
Fragment to your
- The third one is how you'll usually instantiate your
Fragment while passing data to it from your
Activity. When you already have an instance of your
Fragment running you'll have to fall back on your first method.