mortar-flow-example-app icon indicating copy to clipboard operation
mortar-flow-example-app copied to clipboard

onActivityResult might be fired too early.

Open creativepsyco opened this issue 9 years ago • 2 comments

Hi,

I came here looking for a solution to the Activity Result Issues I am having with mortar and flow. And I almost have a solution similar to the LifecycleOwner that you implement in the app. However, instead of firing presenter callbacks in onActivityResult of the Activity I do them in the onResume (for the case when Activity isn't destroyed/recreated) and onAttachedToWindow (for the case when the activity is completely destroyed) of the Activity. I don't know if that is right approach, but eager to hear thoughts over this issue.

So I tried the sample,

And If I run the device in destroy activities mode, then the view may not be attached to the presenter in time for the onActivityResult to be fired. Since the onActivityResult result is fired before the view is completely attached therefore it is null when the presenter tries to access it.

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == SELECT_PICTURE && data != null) {
        try {
          final Uri imageUri = data.getData();
          // Crashes on the line below, because view not attached to the presenter yet.
          Activity activity = activityHelper.findActivity((ContextWrapper) getView().getContext());
          final InputStream imageStream =
              activity.getContentResolver().openInputStream(imageUri);
          final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
          getView().setImage(selectedImage);
        } catch (FileNotFoundException e) {
          Toast.makeText(getView().getContext(), "Failed to load image", Toast.LENGTH_SHORT).show();
        }
      }
    }

One possible solution maybe to cache the result and use it during onLoad (or alternatively during the takeView()) call. But so far doesn't seem to be working, any thoughts for a hook to be inserted to inform about the takeView call, thus informing the presenter that the view is attached and ready for use.

Thoughts?

Cheers.

creativepsyco avatar Aug 04 '15 04:08 creativepsyco

hi @creativepsyco - sorry for the delayed response. Yes, I have handled this case before by caching / storing the onActivityResult data and then check in onResume (I believe) if there is one, fire the callback. This, as you point out, is only necessary when the activity is newly created and thus the view is not attached/subscribed yet.

I'll see about updating the example.

thorbenprimke avatar Nov 01 '15 21:11 thorbenprimke

I could do it plus I can add support for the permission stuff, then the presenters can have access to those as well ;)

creativepsyco avatar Nov 03 '15 08:11 creativepsyco