5

I had a layout for Fragment to show information about a product but sadly, during the creation of fragment there was slight lag( glitch) of around 50ms (that is what I guess how log the lag is which is a lot as the refresh rate for android is 16ms) but when I use the same layout in Activity directly and applied the same logic it looked and felt smooth.

Are there any particular reasons for this case ? Are there any way to make fragment look as smooth as activity during creation ?

You can test some complex layout and try inflate it as fragment's view and using same as layout content for activity.

This is how my oncreate looks like in both fragment and activity:

@Override
public void onCreate ( Bundle savedInstanceState ) {  // or equivalent  override for fragment.
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.fragment_product_profile );
    initLayout ();
    loadData ();
    initCustomMadeImageSlider ();
    autoScrollViewPager ();
}

This is the general idea about the UI

erluxman
  • 18,155
  • 20
  • 92
  • 126
  • I reckon Fragment can never be created faster than it's host Activity simply because it has to be attached to the Activity. – WenChao Jan 13 '17 at 05:33
  • 1
    That sounds like a fine logic to me, but in this case the activity is already created and I just want to attach new fragment in it but its faster to create new activity with same content – erluxman Jan 13 '17 at 05:58

1 Answers1

5

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.

Fragment : Major Advantage is

A separate Activity is created for each form factor with the non-UI details duplicated or otherwise shared across each Activity

Fragments eliminate this problem by taking on the UI details and leaving the other responsibilities to the Activity. This way a separate Fragment can be created for each form factor with the form factor specific UI details being the only responsibilities of each Fragment.

Are Activity faster to create than fragment ? YES . Activity->Fragment .

A Fragment represents a behavior or a portion of user interface in an Activity

Please read about When to use Fragments vs Activities

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198