Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragments and its influence on the performance

I have an Activity with many, many views (200+) and now I'm separating it into fragments in order to decouple my code a bit. The question is - how does it influence on the performance?

  • Let's say I have N views hierarchically layed out in one layout and same amount of views separated in fragments - does it matter in terms of performance?
  • When I hide a fragment, hopefully its views are not being drawn? (so I can gain here).

Generally, if you could give me a link to some book / article about fragments performance I'd be very glad (googled all the Internet with no results).

like image 394
Fenix Voltres Avatar asked Mar 02 '12 09:03

Fenix Voltres


People also ask

Why are fragments important on Android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

Are fragments faster than activity?

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.

What is a fragment give the uses of fragments in Android?

A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.


1 Answers

Fragment is only something like sub-activity with own life cycle. So if you don't need display all view at the same time it's much better use fragments (or multiple activities). For decoupling code you can also create own view components. Main reason to use fragments is better phone/tablet compatibility. Or for elegant way how to swich only part of screen like in ViewPager.

But fragment increase memory usage of application. Particularly if you will keep multiple fragments in memory.

Best way is investigate implementation of fragments, its open source. Or simply add Log.d() to the methods to see what was called.

like image 58
ATom Avatar answered Oct 03 '22 10:10

ATom