[英]onBackPress() is not working when app comes to foreground from background
我的活動中有一個 videoView。 當應用程序從后台進入前台以及由於 videoView.seekTo(1) 而鎖定我的手機並解鎖它時,onBackPress() 不起作用。 我們如何解決它
下面是我的代碼。 請幫忙
public class VideoPreviewActivity extends implements View.OnClickListener {
TextView tv_response;
ImageView iv_video_preview;
VideoView videoView;
@SuppressLint({"ClickableViewAccessibility", "InlinedApi"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_preview);
processIntent(getIntent());
}
public void processIntent(Intent intent) {
int is_doc_context = intent.getIntExtra(IS_DOC_CONTEXT, 2);
if (is_doc_context == 1) {
final MediaController mc = new MediaController(this);
mc.setVisibility(View.GONE);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
final Uri video = Uri.parse(videoFileString);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.seekTo( 1 );
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
}
}
@Override
protected void onResume() {
videoView.seekTo( 1 );
super.onResume();
}
@Override
protected void onPause() {
if (videoView.isPlaying()){
videoView.stopPlayback();
}
super.onPause();
}
@Override
public void onBackPressed() {
LibUtils.onBackButtonClick(VideoPreviewActivity.this);
}
}
如果您需要任何信息,請告訴我。
在您的覆蓋 onBackPressed() 方法中調用 super.onBackPressed() 內部。
您沒有為videoView
調用findViewById
。
1) 確保在xml
的videoView
中添加一個id
:
<VideoView
android:id="@+id/video_view"
android:layout_height="......"
android:layout_width="......." />
2) 在onCreate()
中執行此操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_preview);
processIntent(getIntent());
//here find the video view
videoView = findViewById(R.id.video_view);
}
3)在你的onBackPressed()
中還有一件事:
@Override
public void onBackPressed() {
LibUtils.onBackButtonClick(VideoPreviewActivity.this);
//add this
super.onBackPressed();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.