Are Fragments and Fragment Activities inherently faster than Activities?

一曲冷凌霜 提交于 2019-12-22 05:04:38

问题


Are Fragments and Fragment Activities inherently faster than Activities?

If I don't need to load my activity in fragments, should I be using FragmentActivities and Fragments over Activities?

Reason I am asking is because I have been using Activities, exclusively, for years, and the Facebook SDK as well as Google Maps 2.0 have forced me to use Fragments, and I wonder now if they are inherently "better" or not, versus some other implementation.

If this "not constructive" or "too open ended" then obviously the answer is "no". But if there are some Google developer documents or blog on this exact subject, then I would like to be aware of it


回答1:


I became a believer in Fragments in my last application. Whether or not they are computationally faster, they feel faster because you can swap them in and out basically instantaneously, including full support for the back stack if you do it right (call addToBackStack() on the transaction, or something very similar).

I now use Fragments / Fragment activity for all navigation I want to feel very quick, like clicking on a row to get more details. I only launch new activities for when I want to do a fundamentally different thing and have a clean slate to work with. For instance, I usually have a LoginActivity that deals exclusively with logins/registrations, and at least one more that is the core of the app.

But the fundamental benefit of Fragments still remains their flexibility. I can show fragments on top of other fragments, re-arrange them on different screen sizes, etc. But there are loads of other benefits. It just takes a while to feel natural (just like Activities did at first).

One caveat, I always regret embedding fragments in my layouts. I can't give exact reasons here off the top of my head, but essentially you just lose some flexibility. Instead, I build a normal layout for each fragment, and add a placeholder view in the activity layout, create the fragment programmatically, and use transaction.replace() to add it to the layout. Perhaps because this is the main way I swap fragments in and out of that placeholder view, and prefer to just have a single way of doing things where possible.




回答2:


yeah, fragments are introduced exclusively for supporting large screens to use the area efficiently.handling fragments is very easy and in terms of memory.but nested fragments makes trouble




回答3:


Fragments are very useful if you want to split a screen. So you can have different views within the same screen. Another way of using fragments, lets say you've got tabs to categorise items. Could have clothes, shoes as your tabs. Each tab will have a fragment to hold the products. The tabs could either held in activity or a fragment. I do find fragments a slightly faster than activities but really its not something you would really notice in most cases. Regardless if they was intended for speed or not they still seem/feel little quicker.

The downside of using fragments, is certain callbacks like onBackPressed is only in an activity. Fragments has no access to this. I often find its best to minimise how much activities as possible. Also don't forget Activities aren't just views, they're also a screen. Whereas fragment is only a view and doesn't have a screen. tool/action bars etc. are also only for Activities however if your using a custom toolbar, you can use this in a fragment by implementing onTouch(if not button but some object) or onClick(buttons) the method for these will give you what you need. So really there are some drawbacks but there is almost a workaround for at least some of them.

I do agree Fragment transition is awesome and pop the stack when working with back buttons and onBackPressed Does the trick.

I always use a switch in parent activity etc. to see, which fragment needs to be in view, i often update it using interface passing bundle etc. Not sure if anyone else has found a more efficient way or not. But I do find it really useful when switching views.

Yep fragments are top brass for most things.



来源:https://stackoverflow.com/questions/16451063/are-fragments-and-fragment-activities-inherently-faster-than-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!