Why should I use fragment in Android?

后端 未结 3 838
既然无缘
既然无缘 2020-12-20 13:48

For very long time, I think what is the reason of using fragment in Android if I just develop the application for Android Phone only but not 10.1.

3条回答
  •  無奈伤痛
    2020-12-20 14:05

    From documentation

    You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

    Some advantages are..

    1. A particular UI part, once done in fragment, can be reused in same/different activities.
    2. You can separate different sections of UI, hence code will be neat, and easy readable.

    The ability of fragment to be able to reuse is very helpful when you are creating applications for different kind of android devices (phones, tablets). A well designed fragment can be just plugged into your UI hierarchy.

提交回复
热议问题