问题
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:
- Android - VideoView requires to press BACK twice, in order to exit (adding the onKeyDown function didn't help)
- How to close Landscape VideoView Activity properly? (I have overridden the onDestroy, onResume and onPause methods but that hasn't solved the issue)
Could someone please explain to me what is it that I am doing incorrectly (or failing to understand).
Thanks
回答1:
you can call activity finish() to force to close the activity
回答2:
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.
来源:https://stackoverflow.com/questions/30679583/how-to-close-a-videoview-activity-currently-have-to-press-back-twice