简体   繁体   English

快速问题:碎片太多会使应用程序变慢吗?

[英]Quick question: Can too many fragments make an app slow and how many?

I can't seem to find an answer for that. 我似乎找不到答案。 How many fragments can an app have, or how many xml layouts in general, before it starts getting cluttered and slow? 在开始变得混乱和缓慢之前,一个应用程序可以具有多少个片段,或者通常具有几个xml布局? All I found was that with too many nested layouts the activity itself performs worse. 我发现的是,嵌套布局太多时,活动本身的效果会更差。

Yes, theoretically it can. 是的,理论上可以。 But it's not the number of fragments which can make an app slow, it's the way you use them. 但这不是使应用程序变慢的片段数量,而是使用它们的方式。 Even 2 fragments, if badly used, can make an app slow. 如果使用不当,即使是2个片段也会使应用变慢。 On the other hand, tens of fragments could be handled fine. 另一方面,可以处理数十个片段。 If your app instead needs 50, or 100 fragments, unless it's a really complex app and you're on top of it, then it's a good indicator that you're doing something wrong, either in the app flow, or the design. 如果您的应用程序需要50或100个片段,除非它是一个非常复杂的应用程序,并且您位于其中,那么这可以很好地表明您在应用程序流程或设计中做错了什么。 Android Studio provides you very good tools for profiling an app, use them, see where your bottlenecks are, and fix them. Android Studio为您提供了很好的工具,可用于对应用进行性能分析,使用它们,查看瓶颈所在并进行修复。 Measure the improvements before and after the fix. 在修复之前和之后评估改进。

No, there is no limit on making any number of fragments in Android app. 不,在Android应用程序中制作任意数量的片段没有限制。 And it does not harm any app if you make hundreds of fragments. 而且,即使您创建数百个片段,也不会损害任何应用程序。 But the way you are using those it DOES MATTER . 但是,使用它们的方式确实很重要 As far as the matter of nested layout is concerned, yes it all depends upon your hierarchical level. 就嵌套布局而言,是的,这完全取决于您的层次结构级别。 Suitable approach should be used. 应该使用合适的方法。 Obviously not all layouts you will be showing in your activity. 显然,并非所有布局都会在活动中显示。 On depends or in certain conditions you will be using different nested layout. 根据情况或在某些情况下,您将使用不同的嵌套布局。 If this is the case then you can use fragment for dynamically update the UI or the Activity or Secondly you can dynamically add the views in your activity on demand. 如果是这种情况,则可以使用片段来动态更新UI或活动,或者其次可以根据需要在活动中动态添加视图。 All at once if you are going to show complex nested layouts and those are in deep as well, this can cause sometimes some jerk or flick to load.To overcome this, You need to first think about weather it is necessary to load all the views else load on demand. 如果要立即显示复杂的嵌套布局,并且布局也很深,则有时可能会导致某些混乱或轻拂。要解决此问题,需要首先考虑天气,有必要加载所有视图否则按需加载。 Hope that helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM