简体   繁体   English

当应用程序从后台进入前台时,onBackPress() 不起作用

[英]onBackPress() is not working when app comes to foreground from background

I have a videoView in my activity.我的活动中有一个 videoView。 onBackPress() is not working when app comes to foreground from background and also when i lock my phone and unlock it because of videoView.seekTo(1).当应用程序从后台进入前台以及由于 videoView.seekTo(1) 而锁定我的手机并解锁它时,onBackPress() 不起作用。 How do we solve it我们如何解决它

Below is my code.下面是我的代码。 Please help请帮忙

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);
    }
}

Please let me know if you need any information.如果您需要任何信息,请告诉我。

call super.onBackPressed() inside in your override onBackPressed() method.在您的覆盖 onBackPressed() 方法中调用 super.onBackPressed() 内部。

You didn't call findViewById for videoView .您没有为videoView调用findViewById

1) Make sure that in your xml of videoView you add an id : 1) 确保在xmlvideoView中添加一个id

<VideoView 
android:id="@+id/video_view"
android:layout_height="......"
android:layout_width="......." />

2) Do this in onCreate() : 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) One more thing in your onBackPressed() : 3)在你的onBackPressed()中还有一件事:

   @Override
    public void onBackPressed() {
        LibUtils.onBackButtonClick(VideoPreviewActivity.this);
        //add this
        super.onBackPressed();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序来自后台时询问PIN码 - Ask PIN when app comes from Background 当应用程序出现在前台时执行操作 - Perform an Action when the app comes to foreground 为什么前台服务从后台删除应用程序后无法正常工作? - Why foreground Service not working after removing app from the background? 当应用程序来自后台时更改菜单图标(在onResume()方法中) - Change Menu icons when app comes from background (in onResume() method) HomeFragment 中的 onBackPress 时关闭应用程序的程度 - How close app when onBackPress in HomeFragment 当应用程序移至后台时,在前台服务中获取位置更新不起作用 - getting location updates in foreground service isn't working when app is moved to background Android (java): OnBackPress() 在尝试将 go 从活动返回到片段时关闭我的应用程序 - Android (java): OnBackPress() closes my app when trying to go back from Activity to fragment Android:地理围栏在前台工作但在后台不工作时工作正常 - Android: Geofencing is working fine when its in foreground but not working in background 尝试在后台/前景中运行时应用程序崩溃,但适用于其他手机 - App crashes when trying to run in background/foreground but works for other phones 每当应用程序来自android中的后台时,启动“第一屏幕” - Start the First screen each time app comes from background in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM