ViewPager + SurfaceView = long delay in transition to activity

Basically what the name says,

My application consists mainly of a ViewPager that uses a FragmentStatePagerAdapter

The problem occurs when I add a series of SurfaceViews to the FragmentStatePagerAdapter. Just for testing purposes, I did not subclass SurfaceViews in any way. When I move through several pages, hit "home", and then return to activity, the whole device freezes for a second or two before rendering anything. I can start and end () the application several times during the time it takes to resume after it has been removed from the back. I know the application is running because several AsyncTasks are almost done by the time the first screendraw is complete. I know that these AsyncTasks do not cause problems because I deleted them without effect.

When I change SurfaceViews to Views , the problem completely disappears.

This problem occurs when I use my actual SurfaceViews (which are not negotiable for my project). This issue also occurs on multiple devices.

At this point, I think the error lies directly with the ViewPager, but I don’t know what to do with it, because I need this interface component.

Any help would be greatly appreciated!

EDIT:

To clarify, SurfaceViews / Views represent a view of the fragment added to the FragmentStatePagerAdapter.

+4
source share
2 answers

Found a solution for any future googlers!

Here is how I did it:

In my snippet, I created an instance of mySurfaceView in onCreate ()

Then I attached a dummy layout (LinearLayout) to the actual view during onCreateView ()

Then in onResume () I cleared the mannequin with removeAllViews () and then called addView (mySurfaceView)

This, apparently, allows the ViewPager to quickly build its layout, and then the SurfaceView just appears before the user really sees anything!

+5
source

The best solution I have found is

  • Override getItem() in FragmentPageAdapter
  • When returning a Fragment create a 2 second time delay using the Handler

So that every time your fragment page adapter tries to return to SurfaceView , it will wait 2 seconds or something else. And Surface View will be launched at this time. shown below

 fragmentAdapter extends FragmentPageAdapter 

...

 @Override getItem(int position) 

...

 if (position == 0) > final Handler handler = new Handler(); > handler.postDelayed(new Runnable() { > @Override > public void run() { > // initiate SurfaceView > // return fragment after 5s > } > }, 5000); 
0
source

All Articles