Search code examples
androidandroid-intenttabactivityandroid-tabactivity

Intent of a tab does not stop after switching to another tab in Android


I am having two tabs to show two different TV channels, but when I switch between tabs, their activity(intent) keep running, and they mix up. how can I make sure one channel stops playing before the new one starts?

Here is the code for Activity class:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoview);
    extras = getIntent().getExtras();
    progressDialog = ProgressDialog.show(TvMenuOne.this, "TV1",
            "Please Wait..", true, true);
    PlayVideo();
}

private void PlayVideo() {
    try {
        videoView = (VideoView) findViewById(R.id.surface_view);
        if(extras.getString("id").equals("one")){
            videoView.setVideoPath(pathL);
        }
        if(extras.getString("id").equals("two")){
            videoView.setVideoPath(pathM);
        }

        videoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
        videoView.setMediaController(new MediaController(TvMenuOne.this));

        videoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
                videoView.start();
            }
        });

    } catch (Exception e) {
        progressDialog.dismiss();
        System.out.println("Video Play Error :" + e.toString());
        finish();
    }
}

Solution

  • I've solved it by overriding onPause method, looks like the activity goes to the Pause mode before displaying the new intent.

    @Override
    public void onPause() {
        super.onPause();
        videoView.stopPlayback();
        videoView.setVisibility(2);
    }