onActivityResult is not being called in Fragment
The hosting activity overrides onActivityResult()
, but it did not make a call to super.onActivityResult()
for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult()
call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult()
for all unhandled results, the fragment got a shot at handling the result.
And also from @siqing answer:
To get the result in your fragment make sure you call startActivityForResult(intent,111);
instead of getActivity().startActivityForResult(intent,111);
inside your fragment.
android onActivityResult is not being called in Fragment
I think you are calling startActivityForResult in IabHelper class.
You should call Fragment.startActivityForResult not Activity.startActivityForResult.
To do that in class IabHelper you must implement two methods one with activity param and one with fragment param
onActivityResult not call in the Fragment
The reason why this doesn't work is because you are calling startActivityForResult()
from within a nested fragment. Android is smart enough to route the result back to an Activity
and even a Fragment
, but not to a nested Fragment
hence why you don't get the callback.
(more information to why that doesn't work here or on stackoverflow)
Now in order to make it work I suggest you manually route the callback to the ChildFragment
(=UploadType
) in the ParentFragment
(=BaseContainerFragment
):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment uploadType = getChildFragmentManager().findFragmentById(R.id.container_framelayout);
if (uploadType != null) {
uploadType.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
onActivityResult from Activity to Fragment
You are calling Matisse.from()
on the Activity
which you are getting from the getActivity()
method, tit must be returning you your MainActivity
and the onActivityResult
will be called for the MainActivity
where you are doing nothing and calling super.onActivityResult()
.
Possible Solutions: .
- Shift your
onActivityResult
code logic from yourAddMessageFragment
toMainActivity
- Change you
Matisse
call fromMatisse.from(getActivity())
toMatisse.from(this)
, because looking at the source code ofMatisse
it also supports Fragment context.
Using the second solution you should get your onActivityResult
callback in the fragment and you won't need to change/shift any other code logic.
Hope this helps :)
onActivityResult not called in fragment android
Override onActivityResult
in parent Activity i.e. parent of all fragment
Related Topics
Android Studio - How to Find Valid Certification Path to Requested Target
Retrofit 2.0 - How to Get Response Body for 400 Bad Request Error
How to Enable Location Access Programmatically in Android
React-Native: Images Not Showing in Android Device; But Shows in Emulator
React Native: Determine Number of Lines of Text Component
Android - How to Make a Button Click Play a Sound File Every Time It Been Pressed
Android Studio: Emulator Is Running But Not Showing Up in Run App "Choose a Running Device"
How to Completely Disable Selinux in Android L in the Init.Rc File
How to Delete Shared Preferences Data from App in Android
Recyclerview Cannot Be Scrolled
Android: How to Propagate Click Event to Linearlayout Childs and Change Their Drawable
Retrofit2 - How to Upload Image Using Retrofit in Multipart
Fastest Way to Upload Multiple Image to Server in Android
How to Create a Folder in Android External Storage Directory
How to Turn Off Suggestions in Edittext
Error "Gradle Sync Failed: Connection Refused: Connect"
Android Sqlite: How to Retrieve Specific Data from Particular Column