Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Dangerous to build scopes in getSystemService? #155

Open
rjrjr opened this issue May 4, 2015 · 2 comments
Open

Dangerous to build scopes in getSystemService? #155

rjrjr opened this issue May 4, 2015 · 2 comments
Assignees

Comments

@rjrjr
Copy link
Collaborator

rjrjr commented May 4, 2015

@loganj, didn't you say that this was a bad idea? How come?

https://github.com/square/mortar/blob/master/mortar-helloworld/src/main/java/com/example/hellomortar/HelloApplication.java#L25

@loganj
Copy link
Collaborator

loganj commented May 4, 2015

Building a root scope this way is fine. It's dangerous to try to access the Application from getSystemService in an Activity, because getSystemService is called when the Activity is not (yet) attached to the Application. At least, that's the behavior I was seeing. I haven't dug into it much.

This is the guard I ended up with:

  @Override public Object getSystemService(String name) {
    if (getApplication() == null) {
      return super.getSystemService(name);
    }
    MortarScope appScope = MortarScope.getScope(getApplication());
   // ...
  }

@pyricau
Copy link
Member

pyricau commented May 5, 2015

Got the same behavior when overriding getResources() and calling getApplication() from there. Ended up with same guard.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants