Ali Yucel Akgul
Ali Yucel Akgul

Reputation: 1123

What to choose: activity or viewpager(with fragments)

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

Answers (2)

Andrew Alcock
Andrew Alcock

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:

  • When the ViewPager is first shown, the CPU will be more active since it has to instantiate the Fragment being shown and those off-screen to the left and right (2x or 3x the equivalent work)
  • Since all the Fragments stay in memory, memory use will be (slightly) higher. If your Fragments don't use much memory, the difference in memory usage is trivial.
  • If there is memory pressure (or "Do not keep Activities" is on), then only 2 or 3 Fragments are kept; other fragments are suspended so you need to ensure the 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
  • Once the Fragments are instantiated, as the user slides from side to side, CPU use will be lower and (most importantly) the UX will be much smoother since the Views are already present and rendered in memory.
  • Communication between Fragments is relatively easy using the parent Activity as the communication hub.

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

user2652394
user2652394

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

Related Questions