Use Startactivityforresult from Non-Activity

call to startActivityForResult() from non activity class and getting result in existing activity or fragment

My all code is in fragment. But I don't think so it cause this problem

It's the problem.

If you call Activity#startActivityForResult(), you receive onActivityResult in activity.

If you call Fragment#startActivityForResult(), you receive onActivityResult in that fragment.

(How this is done under the hood is that FragmentActivity encodes a fragment index to the request code for those calls where the result ought to go to a fragment.)

So, calling startActivityForResult() in an activity and expecting to see the result in a fragment does not work. Either move the request to fragment, or the response handling to activity.

Also, if you don't handle a result, pass it on to the super implementation.

How to launch activity from non-activity class and receive the onActivityResult() on the activity class

Looking close to the answer that I post on my question: call to startActivityForResult() from non activity class and getting result in existing activity or fragment

I've noticed that the problem is that I was calling startActivityForResult() from Activity, and should be called from the fragment. So instead passing Activity to my class, I refractored like the answer to use Fragment.

So fragment code will look like:

public class MyFragment extends Fragment
{
private static final String TAG = "Example";
private NonActivityClass nonActivityClass = null;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
RequestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
nonActivityClass = new NonActivityClass(this); //this, instead of getActivity()
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d(TAG, "ON RESULT");
if (requestCode == NonActivityClass.REQUEST && resultCode == RESULT_OK)
{
Log.d(TAG, "ON RESULT!");
}
}
}

And non-activity class like:

public class NonActivityClass
{
private Fragment fragment;
static final int REQUEST = 1;

public NonActivityClass(Fragment fragment)
{
this.fragment = fragment;
}
private Activity getActivity()
{
return this.fragment.activity;
}

public void DispatchTakePictureIntent()
{
Log.d(TAG, "DISPATCH");

PackageManager packageManager = getActivity().getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY))
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(packageManager) != null)
{
// Create the File where the photo should go
File photoFile = null;
try
{
photoFile = CreateImageFile(); //this method creates the temporary file, shouldn't be relevant
}
catch (IOException ex)
{
Log.d(TAG, "Error occurred while creating the File:" + ex.toString());
}
// Continue only if the File was successfully created
if (photoFile != null)
{
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.example.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

//THIS
fragment.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
//
}
}
}
}
}

get onActivityResult for Non Activity Class (Android)

If you need the result outside of an Activity or Fragment, your Activity class will need to override onActivityResult, then pass that result to your other class by method call.



Related Topics



Leave a reply



Submit