Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Activity faster to create than fragment?

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

like image 530
erluxman Avatar asked Jan 13 '17 05:01

erluxman


People also ask

Can I add fragments to an activity while it is running?

We can add or remove fragments in an activity while the activity is running. It is possible to develop a UI only using Activities, but this is generally a bad idea since their code cannot later be reused within other Activities, and cannot support multiple screens.

What is the difference between Android fragment and activity?

When Android was first made back in 2008, Activity is there as a screen to hold all UI views that the user interacts with. If developer wanted 2 different screens, the usual way is to have 2 activities Beginning Android 3.0, Fragment was introduced… It was mainly to cater for the need of tablet, which have a much wider screen than a phone.

What is an example of a fragment in an activity?

Good example is the ViewPager in an activity, which may hold 3 fragments displaying the same data in multiple ways (lists, tiles, detailViews).

What is the best way to share data between fragments?

I find that communicating between fragments while their activity is open allows for faster responses and the effective sharing of data, since the data only needs to be instantiated once, in the parent activity. just my 2c. Actually, FragmentPager would be better for holding fragments. There's a lot of conflicting information in the answers here.


1 Answers

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

like image 167
IntelliJ Amiya Avatar answered Sep 27 '22 23:09

IntelliJ Amiya