79

The Fragment and Custom View can achieve the similar function, I know that fragment is more re-usable comparing with custom view, any other benefits/enhancements for using Fragment? Is fragment supposed to replace Custom View, or just a enhancement for some specific purpose?

For instance, the code below is fragment:

public class TestFragment extends Fragment {

    private TextView tv_name;
    private Button btn_play;
    private Button btn_delete;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.testfragment, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        tv_name = (TextView)getView().findViewById(R.id.tv_name);
        btn_play = (Button)getView().findViewById(R.id.btn_play);
        btn_delete = (Button)getView().findViewById(R.id.btn_delete);

    }
}

The code for custom view:

public class TestCustomView extends LinearLayout {

    private TextView tv_name;
    private Button btn_play;
    private Button btn_delete;

    public TestCustomView(Context context, AttributeSet attrs){
        super(context, attrs);

        setOrientation(LinearLayout.HORIZONTAL);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        tv_name = new TextView(context);
        addView(tv_name);

        btn_play = new Button(context);
        addView(btn_play);

        btn_delete = new Button(context);
        addView(btn_delete);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.testfragment, container, false);
    }
}

Both TestFragment and TestCustomView can create a view consisting of TextView and Buttons, and use tags of Framelayout/fragment and com.packagename.TestCustomView to declare in the activity's xml layout file, but what's the advantages to use Fragment?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Kai
  • 916
  • 1
  • 7
  • 10
  • I find fragments useful when you need access to something the View does not have, e.g. information about the Fragment lifecycle, or something like 'LoaderManager'. – Karakuri Jul 05 '15 at 23:42

4 Answers4

42

Fragment can be used in different scenarios but most used are:

  • wrapper around a view
  • headless fragment - i.e. no view => not very helpful in general but can be used
  • retainable fragment - can be any of above. By using Fragment.setRetainInstance(true) you can bypass Fragment.onDestroy(), i.e. can keep fragment data on configuration changes but fragment view structure is still destroyed/recreated
  • can be added to activity back stack, i.e. easy Back button previous state restore

There are cases where fragment are complete pain in the neck, then there are cases where they can achieve results quicker.

For some custom and more flexible situations fragments can get cluttered and managing them would be difficult. So dealing with views directly can be really handy and more helpful for some cases. But everything is based on requirements.

Note View has its own life cycle too and can store/recreate saved instance state. A little bit more work but it has the option too.

Dimitar Genov
  • 2,056
  • 13
  • 11
  • 5
    Thans for the answer. It would be nice to see some specific examples that demonstrate pros and cons of using fragments over views and vice versa to help better understand what approach is more suitable. Currently, it looks to me like fragments are the way to go but again some examples might help to understand possible drawbacks of this approach. – vir us Oct 22 '17 at 17:23
26

Custom Views have the advantage of simplicity and their primary purpose is to display a piece of data on the screen. They must rely on other components in order to do more.

Think of Fragments as a functional unit, a way to display a portion of UI that has a specific purpose, using one or more Views. Fragments are connected to the Activity lifecycle and they can include and control Loaders to populate the Views with data. They can also include sub-fragments. Finally, they can also be added to a synthetic back stack. They can do many things and are somewhat complex to learn.

As you can see, Fragments have much more in common with Activities than they have with custom views.

As a side note, Fragments can also be headless (with no UI). Headless fragments provide a way to encapsulate non-visual functionality relying on the Activity lifecycle in a separate component.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • 10
    All true, but that doesn't explain what's the advantage of using a fragment over custom-view – Nir Alfasi Jul 06 '15 at 00:13
  • 3
    In the example given in the question there would be no advantage because it's just displaying some static data from a layout. In real world examples when you need to build complex interfaces relying on user interaction, load data, create sections in your app, etc. you need to use both at the same time. The point of my answer is you can't compare them on the same level because they have different purposes. – BladeCoder Jul 06 '15 at 00:19
22

Fragments come with their own lifecycle, which can be a hinderance or a bonus, depending on what you need.

Fragments get lifecycle methods like onResume or onSavedInstanceState, which can help you deal with state transitions in your application. If you're using custom views, you need to handle that kind of things on your own.

There are people who advocate against using fragments, I suggest reading https://developer.squareup.com/blog/advocating-against-android-fragments/

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Malthan
  • 7,005
  • 3
  • 20
  • 22
0

The most useful functionality of using Fragments over Custom Views is that they have their own Lifecycle Callbacks, i.e. we can register our own FragmentLifecycleCallbacks to do some operations before/after Fragment creation/destruction.

We can create our own FragmentLifecycleCallbacks and register it with Activity to inject dependencies in Fragment through Dagger. There are some workarounds to inject dependencies in Custom Views too, but doing it through FragmentLifecycleCallbacks is much cleaner and easier to do.