What is the use case for a Fragment with no UI?

后端 未结 5 702
北荒
北荒 2020-12-05 22:48

The Android Developer guide has a decent section on the use of Fragments. One way to use Fragments is without a UI. There are a few references to using this as a means of

5条回答
  •  我在风中等你
    2020-12-05 22:59

    A very useful feature of Headless Fragments

    Headless Fragments, have one really useful feature - they can be retained by the FragmentManager across configuration changes. Since they do not have any UI related to them, they do not have to be detroyed and rebuilt again when the user rotates the device for example. In order to activate this behaviour, one just has to set the retained flag of the Fragment when it is initialized. This can be done in the onCreate method of the Fragment.


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
    

    ref- https://luboganev.github.io/blog/headless-fragments/

提交回复
热议问题