AppAuth-Android icon indicating copy to clipboard operation
AppAuth-Android copied to clipboard

Run AuthService outside of activity context

Open Smedzlatko opened this issue 8 years ago • 8 comments

I suddently ran into a weird issue when running AppAuth in my android app in an emulator. Everything works fine on phone, as far as I am concerned but the following exception is thrown inside emulator:

04-25 11:29:52.582 5103-5103/com.myfuturity.futurity W/System.err: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I tracked the issue down and we are not injecting activity context into the auth state manager and it is apparently needed. We are using KOIN for dependency injection (including context) and all our AppAuth services are injected via Koin.

Is there some workaround that would enable us running AppAuth with application context (that's the one being injected)? Setting the FLAG_NEW_TASK to the auth intent does not work, unfortunately.

Thank you for the clarification.

Smedzlatko avatar Apr 25 '18 09:04 Smedzlatko

We had this same issue and resolved it by using an activity context for a one off instance of AuthorizationService just in our login activity and cleaning it up in onStop. We inject the one based on application context everywhere else.

chenthorne avatar Apr 26 '18 17:04 chenthorne

I should probably expose a variant of getAuthorizationRequestIntent that allows you to specify the completion and cancelation PendingIntents, so that you can call startActivity yourself with the correct context. Alternatively, I could allow the context to be overridden with a parameter passed to performAuthorizationRequest.

iainmcgin avatar Apr 26 '18 19:04 iainmcgin

Hi, What is the recommended way of doing this? I am stuck on this error and can't seem to find a way to get around this.

Manukam avatar Dec 08 '19 18:12 Manukam

Any news?

halysongoncalves avatar May 06 '20 18:05 halysongoncalves

Someone already figured out a workaround? We also encapsulated the entire authorization process into a separate module, while using KOIN to inject the androidContext.

Moes81 avatar May 06 '21 14:05 Moes81

The workaround is already mentioned by @clenthorne: https://github.com/openid/AppAuth-Android/issues/333#issuecomment-384731640

You need to maintain two different flavours of AuthorizationService: One that takes an Activity and is recreated for each new Activity instance. This one is used to launch Custom tabs. One that takes application context and is created once for the app. This one is used for making authorized network requests.

If you are using a DI framework you can solve this by using two different Qualifier annotation classes for the different flavours.

rasmusohrstig avatar Jun 16 '21 14:06 rasmusohrstig

We had this same issue and resolved it by using an activity context for a one off instance of AuthorizationService just in our login activity and cleaning it up in onStop. We inject the one based on application context everywhere else.

this approach is not working for me, i can't bypass the crash at all

Omara8 avatar Aug 25 '21 15:08 Omara8