Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager + SurfaceView = long delay when navigating back to activity

Basically what the title says,

My application consists primarily of a ViewPager which uses a FragmentStatePagerAdapter

The problem occurs when I add a series of SurfaceViews to the FragmentStatePagerAdapter. Just for testing purposes I did not subclass the SurfaceViews in any way. When I navigate a few pages in, hit "home," and then return to the activity, the entire device freezes for a second or two before rendering anything. I can launch and finish() the app several times in the time it takes for it to resume after being pulled off of the backstack. I know that the application is running because several AsyncTasks have nearly finished by the time the first screendraw finally occurs. I know that these AsyncTasks do not cause the problem because I have removed them to no effect.

When I change the SurfaceViews to Views the problem vanishes entirely.

This problem occurs when I use my actual SurfaceViews (which are non-negotiable for my project) as well. This problem also occurs on multiple devices.

At this point I think the fault lies squarely on the ViewPager, but I don't know what to do about it because I need that interface component.

Any help would be greatly appreciated!

EDIT:

To clarify, the SurfaceViews/Views represent the View of the Fragment which was added to the FragmentStatePagerAdapter.

like image 546
ebolyen Avatar asked Jul 06 '12 05:07

ebolyen


1 Answers

Found the solution for any future googlers!

Heres how I did it:

In my fragment I instantiated mySurfaceView in onCreate()

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

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

This seems to allow the ViewPager to build its layout quickly and then the SurfaceView is just popped in before the user actually needs to see anything!

like image 124
ebolyen Avatar answered Oct 18 '22 13:10

ebolyen