Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

android - Fragment 2 fragment communicating

Why i should use the Communicating with Other Fragments pattern when I could simply use

((MyActivity)getActivity()).doFoo();

In my Fragment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is ultimately a question of coupling and re-usability.

If you use the code example you posted in your question your Fragment is not re-usable with other activities. It is 'tightly coupled'. It's fine if you're working on your own and you don't think anyone is likely to ever use or read your code later (including yourself), and that you're definitely never going to need to use that Fragment anywhere else. But generally speaking you should still use the proper pattern, just to get into the habit of using it.

If you have a position coding in a company keeping 'loose coupling' will help you and your co-workers a great deal in the long run, and means you can go straight back to your code and re-use it on later projects if called for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...