Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - take picture - onActivityResult returns immediately

In my App I would like to take a picture. The current Fragment is within a TabHost.

Within a fragment I start the camera action with:

Intent takePicture = new Intent( MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult( takePicture, 1);

In my MainActivity I have the onActivityResult:

@Override
public void onActivityResult( int requestCode, int resultCode, Intent imageReturnedIntent) {
       super.onActivityResult( requestCode, resultCode, imageReturnedIntent);
       if( imageReturnedIntent == null) { 
           // immediately coming here when my OWN app is singleTop or singleTask

When my App contains an Activity with launchmode singleTask or singleTop, then onActivityResult immediately returns with imageReturnedIntent is null. When I remove the launchmode in my App, then it works again.

How can I fix this?

What I read on the internet is that the activity that is launched (so, in my case the Camera App) should not have a launchmode singleTop or singleTask.

Question: how can my own Activity (having launchmode singleTop or singleTask) get an onActivityResult from a Camera App?

Notice: in Android 5.0+ this works fine. In Android 4.x not.

like image 568
tm1701 Avatar asked Dec 12 '25 13:12

tm1701


1 Answers

The problem is:

A "singleInstance" activity, on the other hand, permits no other activities to be part of its task.

see here https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

and the startActivityForResult launches new activity in the same task.

So, if a task is started for an activity in singleInstance mode, then Android is unable to launch new activity in the task.

You should use singleTask mode instead. Is is much like singleInstance, but allows other activities to be launched on the task.

on android >=5.0 you will have another problem with singleInstance mode: you always have resultCode == Activity.RESULT_CANCELED.

like image 104
babay Avatar answered Dec 14 '25 09:12

babay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!