Is it possible to manually destroy SurfaceView?

My SurfaceView is not destroyed even if onPause triggers an action.

I care about the flow 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 a hack, I have to check the state of the thread in onResume, and if the thread has already completed, I would end the action

 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 thread processing from the Destroyed and surfaceCreated surfaces to onPause () and onResume () activity. Is it possible to manually destroy SurfaceView in onPause () and recreate it in onResume ()?

+4
source share
3 answers

You can add a layout as a parent to represent the surface, and then set the visibility of the GONE layout to onPause () and set VISIBLE to onResume () activity.

+3
source

The dynamic appearance of the surface can be viewed dynamically.

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(); 
+3
source

Yes, it is possible. Initialize the size first

  Size currentSurfaceSize; cameraSurface.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(QR_Reader_Activity.this, new String[]{Manifest.permission.CAMERA}, RequestCameraPermission); permission = true; return; } try { cameraSource.start(cameraSurface.getHolder()); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { currentSurfaceSize = new Size(width, height); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { onPause(); } }); 

where you want to destroy the surface, use this code below.

  if (currentSurfaceSize==null){ cameraSurface = (SurfaceView) cameraSurface.getHolder(); cameraSurface.removeCallbacks((Runnable) cameraSurface); } 
0
source

All Articles