My SurfaceView is not getting destroyed even if onPause of the activity is called.
I am taking care of the thread in
public void surfaceCreated(SurfaceHolder holder) {
if (mGameThread.getState() == Thread.State.TERMINATED) {
createGameThread(getHolder(), getContext());
}
mGameThread.setRunning(true);
mGameThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mGameThread.setRunning(false);
while (retry) {
try {
mGameThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
As an hack I have to check the state of the thread in onResume and if the thread is already terminated, I would finish the activity
protected void onResume() {
Log.d(mLogTag, "onResume()");
super.onResume();
if (mGameThread != null) {
if (mGameThread.getState() == Thread.State.TERMINATED) {
finish();
}
}
}
Unfortunately it is not possible to move the thread handling from surfaceDestroyed and surfaceCreated to onPause() and onResume() of the activity. Is it possible to manually destroy the SurfaceView in the onPause() and recreate it in onResume()?
You can add surface view dynamically on your view.
Example :layout.xml
<FrameLayout
android:id="@+id/fragment_file_videoplayer_surface_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
MainActivity.java
FrameLayout fl_surfaceview_container =
(FrameLayout)findViewById(R.id.fragment_file_videoplayer_surface_container);
// Add surfaceView on Framelayout
SurfaceView videoSurface = new SurfaceView(getActivity());
fl_surfaceview_container.addView(videoSurface);
//if remove or destroy surfaceview
fl_surfaceview_container.removeAllViews();
You can add a layout as a parent of the surfaceview, then set visibility of the layout GONE in onPause(), and set VISIBLE in onResume() of the activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With