Reputation: 1123
For couple of days, I have a question in my mind that couldn't find any answer for. Right now I have an android app that I use 1 fragment and a view pager with 3 fragments. What I wonder is, is there any performance difference between those two? Will using of fragments provide me less cost than using activities? What would be good for RAM/CPU of device while changing layouts; skip from one activity to another or skip between fragments of viewpager? Because if I use activity, than everytime a window will pop up. But in viewpager's fragment I will stay in one activity and will change only pages. Will that provide me performance on device or is just an UI issue?
Thanks in advance.
Upvotes: 2
Views: 823
Reputation: 19651
A ViewPager works in this way: Let's say it has n pages, or Fragments. If you are on index i, Android will ensure Fragments i-1 and i+1 and their Views are instantiated. As you page from side to side, subject to memory and the "Don't keep activities" setting, you will keep instantiating Fragments until all Fragments are present in memory. When you side from side to side, Android uses OpenGL to smoothly slide one Fragment's View over onto the screen while sliding the other out.
Contrary to some other answers, Android does not instantiate all Fragments when the ViewPager starts - instantiation is lazy. In addition to the default behaviour described above, you can control memory use using the setOffscreenPageLimit()
.
This is the key difference from the older Activity stack model which instantiates Activities only when called.
There are several consequences:
onSaveInstanceState()
is implemented correctly. When the user slides back, the Fragments will be resumed. However, resuming a Fragment is less CPU intensive than creating the equivalent new Activity One other point: The ViewPager's UI metaphor is different to the Activity stack. When weighing which approach to take, you probably should value the UX more than the implementation details (CPU and memory) unless the implementation costs are extreme.
Upvotes: 3
Reputation: 1686
There is the difference b/w those 2:
If you use fragments then the first time your activity load, it will load ALL of your fragments as once (a huge memory consumption here if you have many fragments, but in your case, 3 fragment maybe acceptable). But as it load ALL as once, you can move back and forward without delaying (it's obvious for a view pager anyway). So if the requirement allow users to move back and forward your pages then Fragment is the choice, the problem is how you can manage the memory consumption to avoid memory leak or out of memory exception.
If you use activity (many activities) instead of fragments then the memory will stack up every time you move forward and unstack every time you back. It seems more efficient but the problem many occurs if each activity contains a large image which take a bit long to load. Therefore, every time you move back or forward there WILL be a delay which is not nice for user to observe. So, if the requirement only allow user to move forward, the using activity instead of fragment is obviously right.
In conclusion, depending on your application requirements and how you manage the memory issue to use Fragment or Activity. These are only my experiences and observations, I'm not guarantee that's true in any cases. There maybe a better explanation that I haven't know yet. Hope this help and English is not my native language, if you don't understand leave comment below.
Upvotes: 2