rahulthewall
rahulthewall

Reputation: 795

How to close a VideoView Activity (currently have to press back twice)

I have a simple Activity to preview a video in FullScreen. The layout of the Activity is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black"
                android:fillViewport="true">

    <VideoView
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:scaleType="fitCenter"/>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_height"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_alignParentBottom="true"
        <Button android:id="@+id/buttonClose"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:background="@null"
                android:textSize="18sp"
                android:textColor="@color/white"
                android:text="@string/close"/>
    </RelativeLayout>
</RelativeLayout>

The relevant Activity code is:

public class VideoFullscreenActivity extends Activity {
    private VideoView video;
    private Button closeButton;
    private MediaController mediaController;
    private boolean toClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_fullscreen);

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        video = (VideoView) findViewById(R.id.video);
        closeButton = (Button) findViewById(R.id.buttonClose);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.d("VideoPreview", "onClick Close Button");
                VideoFullscreenActivity.super.onBackPressed();
            }
        });

        Bundle bundle = getIntent().getParcelableExtra("bundle");

        if (bundle != null) {
            Uri videoUri = bundle.getParcelable("data");
            mediaController = new MediaController(this);
            mediaController.setAnchorView(video);
            video.setMediaController(mediaController);
            video.setVideoURI(videoUri);
            video.requestFocus();
            video.start();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                finish();
        }
        return (super.onOptionsItemSelected(menuItem));
    }

    @Override
    protected void onResume() {
        Log.i("VideoPreview", "Resume");
        video.resume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.i("VideoPreview", "Pause");
        video.suspend();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.i("VideoPreview", "Destroy");
        video.stopPlayback();
        super.onDestroy();
    }

}

When I press the "Close" button the first time, the sequence is:

D/VideoPreview﹕ onClick Close Button
I/VideoPreview﹕ Pause
I/VideoPreview﹕ Resume
I/VideoPreview﹕ Destroy

At this stage, the Activity doesn't close, it restarts and the video starts playing again. To close the Activity, I have to press the "Close" button again.

When I press the "Close" button the second time, the sequence is:

D/VideoPreview﹕ onClick Close Button
I/VideoPreview﹕ Pause
I/VideoPreview﹕ Destroy

Now, the Activity closes.

Some relevant questions that I found on StackOverflow:

Could someone please explain to me what is it that I am doing incorrectly (or failing to understand).

Thanks

Upvotes: 1

Views: 3581

Answers (2)

rahulthewall
rahulthewall

Reputation: 795

The code to close the activity was correct. The problem was in the Activity which launched the VideoFullscreenActivity. The offending piece of code was:

    <VideoView
            android:id="@+id/tag_attach_video"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"/>

VideoFullscreenActivity did finish on calling onBackPressed(), but the VideoView in the earlier Activity was still active which necessitated pressing back again.

I changed the VideoView to an ImageView and used a placeholder image to indicate the fact that the attached data is a video. That solved the problem.

Upvotes: 0

jackGao
jackGao

Reputation: 32

you can call activity finish() to force to close the activity

Upvotes: 1

Related Questions