Search code examples
androidandroid-activity

Switch activity after camera intent finished


I'm trying to record a video and then start new activity where I can let the user do some processing of the frames. Unfortunately when I accept the video instead of starting the new activity, the camera activity appears again ready to capture new video and only after I press the back button the activity I expected appears.

I checked similar question New activity after camera intent but it seems to me that I've already done what's suggested as an answer.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        runCamera();
    }

    @Override
    protected void onStart() {
        super.onStart();
        runCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        runCamera();
    }

    public void runCamera() {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        takeVideoIntent.putExtra("android.intent.extra.durationLimit", 30);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            Uri videoUri = intent.getData();

            Intent startVideoProcessingIntent = new Intent(MainActivity.this,
                    VideoProcessingLayout.class);

            startVideoProcessingIntent.putExtra("videoUriString", videoUri.toString());
            startActivity(startVideoProcessingIntent);
        }
    }

I'd like to be switch to the videoProcessing activity after video is captured and to be able to use the back button to exit the camera intent (and the application itself since this is the entry point of the app), instead of just loading the intent itself again.


Solution

  • You are calling runCamera() in your onResume() , onResume() is called when your activity becomes visible. This means that after your camera intent is finished, onResume() is called and you start the intent again.

    Remove this call from onResume() and it should work as expected.

    Another point to note is that onStart() is called after onCreate() when the activity is launched for the first time. You are calling runCamera() in both of these which means you are starting the same intent two times. You should remove this call from onCreate() as well and just keep it in onStart().

    Read about Activity Life Cycle functions in documentation.

    To sum it up:

    onCreate():
    fires when the system first creates the activity

    onStart():
    The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.

    onResume(): When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback

    onPause():
    The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed)

    onStop():
    When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen