我想从类finish()VideoPlayer活动RenderView。但是,从finish()调用RenderView不会调用onDestroy()。该活动没有被破坏,并且不会返回到先前的主活动。

public class VideoPlayer extends Activity  {

    @Override
    protected void onPause(){
        super.onPause();
        renderView.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        naClose2();
    }

}


mFinished = true,但是它返回到调用parseServerInfo()的方法finish(),并继续执行其余代码。

编辑

public class RenderView extends SurfaceView implements SurfaceHolder.Callback   {
    private Context mContext;

    private Runnable prDisplayVideoTask = new Runnable() {
        public void run() {
            if(zoomState.isPlaying()==false){
                if(zoomState.getFlag()==FlagType.PAUSE){
                    zoomState.setFlag(FlagType.NONE);
                    naPause();
                }
            } else {
                naStart();
            }
            prVideoDisplayHandler.postDelayed(this, prDelay);
        }
    };

    public RenderView(...) {
        super(_context);
        this.mContext = _context;
        init(address, windowWidth, windowHeight, videoWidth, videoHeight,
                server_ip, server_port);
        SurfaceHolder holder = getHolder();
        holder.setFormat(PixelFormat.RGBA_8888);
        holder.addCallback(this);
    }

    @SuppressLint("NewApi")
    public void init(...) {
        parseServerInfo(receivedData);
        prVideoDisplayHandler.removeCallbacks(prDisplayVideoTask);
        prVideoDisplayHandler.postDelayed(prDisplayVideoTask, prDelay);
    }

    public void pause(){
        naPause();
        prVideoDisplayHandler.removeCallbacks(prDisplayVideoTask);
    }

    public void resume(){
        prVideoDisplayHandler.postDelayed(prDisplayVideoTask, prDelay);
    }

    public void parseServerInfo(String data) {
        if (numCameras == 0) {
            Toast.makeText(mContext, "No stream detected!", Toast.LENGTH_LONG).show();

            // Finish is called here
            VideoPlayer videoplayer = (VideoPlayer) mContext;
            videoplayer.finish();
            return;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCreated  == true) {
            surfaceDestroyed(holder);
        }
        Surface surface = holder.getSurface();
        render(surface);
        mCreated = true;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCreated = false;
    }
}


希望有人可以指出我做错了什么。

最佳答案

这就是为什么它继续进行并冻结的原因(如您的问题的评论中所述)。这应该。这就是Java语言的工作方式。这里,

@SuppressLint("NewApi")
public void init(...) {
    parseServerInfo(receivedData);
    ...
}


您呼叫parseServerInfo(receivedData);

public void parseServerInfo(String data) {
    if (numCameras == 0) {
        Toast.makeText(mContext, "No stream detected!", Toast.LENGTH_LONG).show();

        // Finish is called here
        VideoPlayer videoplayer = (VideoPlayer)getContext();
        videoplayer.finish();
        return;
    }
}


因此,您将看到Toast,获得称呼,并且说好。但是,您不必回头看看自己来自哪里。加上注释,您的init方法应该说的是

@SuppressLint("NewApi")
public void init(...) {
    // make a call to check that the number of cameras is not 0
    parseServerInfo(receivedData);
    // AND CONTINUE NO MATTER WHAT...
    prVideoDisplayHandler.removeCallbacks(prDisplayVideoTask);
    prVideoDisplayHandler.postDelayed(prDisplayVideoTask, prDelay);
}


您需要的是

public boolean parseServerInfo(String data) {
    if (numCameras == 0) {
        Toast.makeText(mContext, "No stream detected!", Toast.LENGTH_LONG).show();

        // Finish is called here... AND FALSE IS RETURNED
        VideoPlayer videoplayer = (VideoPlayer) mContext;
        videoplayer.finish();
        return false;
    }
    return true;
}


然后

@SuppressLint("NewApi")
public void init(...) {
    // make a call to check that the number of cameras is not 0
    // AND CONTINUE IF GOOD (TRUE)
    if(parseServerInfo(receivedData)){
        prVideoDisplayHandler.removeCallbacks(prDisplayVideoTask);
        prVideoDisplayHandler.postDelayed(prDisplayVideoTask, prDelay);
    }
}


这将解析您的数据,完成应完成的活动,然后停止SurfaceView初始化方法的进度。对不起,这么多冗余的代码,但最容易解释的是:P

关于java - 从SurfaceView调用finish()不会破坏Activity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29049031/

10-12 04:11