Soroush
Soroush

Reputation: 89

How to get context in onActivityResult

I need to run a method in onActivityResult which one of its parameters is context. I tried several ways to pass context to that method but none of them worked and the context is considered as null. Is there any way to get context in onActivityResult?

Additional Info: onActivityResult is implemented in a subclass of PreferenceFragmentCompat and getActivity and getContext return null.

Upvotes: 0

Views: 1201

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007624

If your onActivityResult() is implemented on a subclass of Activity, use this, as Activity is a Context.

If your onActivityResult() is implemented on a subclass of Fragment, use getActivity() to get the Activity that is hosting this fragment (and, again, Activity is a Context).

Upvotes: 2

Related Questions