t-artikov
t-artikov

Reputation: 31

Android SurfaceView bug


I have encountered the nasty Android SurfaceView behavior:
When you leave an Activity with SurfaceView and quickly go back, the previous content of SurfaceView doesn't disappear and is displayed under the new one.

See the example project that reproduces the problem, please.
https://github.com/t-artikov/surface-view-bug

I have found that this is an Android bug, which was fixed on Android P
https://issuetracker.google.com/issues/72624889
But I need to get it to work on earlier OS versions.

Will be glad to any ideas how to solve the issue. Maybe someone has already encountered it and knows a workaround.

Upvotes: 3

Views: 996

Answers (1)

greeble31
greeble31

Reputation: 5042

Note: This problem wouldn't occur on an API 22 device. I used the Pixel 2 API 26, as the OP did, in order to reproduce it.

It appears that, under this condition, the SurfaceView saves its current contents to some sort of hidden intermediate surface, at certain times (like when a transition begins). The condition can't be corrected by replacing the SurfaceView, or by adjusting the background drawables of any ancestor views.

Your onDrawFrame() is clearing the buffer with a transparent color, which is letting this "intermediate surface" show through. If you don't need the transparency, you could just clear it to solid white:

GLES20.glClearColor(1, 1, 1, 1);

EDIT

I found myself coming back to this question, as it is a very troublesome bug. Since you need to keep the transparency, it turns out you can add this to SecondActivity, to avoid the problem:

@Override
protected void onStart()
{
    overridePendingTransition(0,0);
    super.onStart();
}

I also tried disabling hardware acceleration, but no luck there.

Upvotes: 1

Related Questions