Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to manually destroy SurfaceView?

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()?

like image 350
Gurpreet Sachdeva Avatar asked Feb 28 '13 11:02

Gurpreet Sachdeva


2 Answers

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();
like image 67
kailash barochiya Avatar answered Nov 18 '22 13:11

kailash barochiya


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.

like image 3
Muhammad Adil Avatar answered Nov 18 '22 13:11

Muhammad Adil