diff options
| author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-07-14 19:26:43 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-07-14 19:26:43 +0000 |
| commit | 3d80a733504ccdfdb2f5cc378efc1cc87b270996 (patch) | |
| tree | c09651d451581dfa34d5a9bc6ed6e53d8391dfde /core/java/android | |
| parent | c4c5d60607a1c1671258371f61502ee1f32d7b70 (diff) | |
| parent | 0b06d4e0bab4c9de241b54991b877c211120be54 (diff) | |
Merge "Fix crash in monodroid apps" into oc-dr1-dev
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/app/ActivityThread.java | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 204df63f600d..abf48a85c45e 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -6480,9 +6480,9 @@ public final class ActivityThread { private <T> T instantiate(ClassLoader cl, String className, Context c, Instantiator<T> instantiator) throws ClassNotFoundException, IllegalAccessException, InstantiationException { - if (c.getApplicationContext() instanceof Application) { - T a = instantiator.instantiate((Application) c.getApplicationContext(), - cl, className); + Application app = getApp(c); + if (app != null) { + T a = instantiator.instantiate(app, cl, className); if (a != null) return a; } return (T) cl.loadClass(className).newInstance(); @@ -6491,14 +6491,25 @@ public final class ActivityThread { private <T> T instantiate(ClassLoader cl, String className, Intent intent, Context c, IntentInstantiator<T> instantiator) throws ClassNotFoundException, IllegalAccessException, InstantiationException { - if (c.getApplicationContext() instanceof Application) { - T a = instantiator.instantiate((Application) c.getApplicationContext(), - cl, className, intent); + Application app = getApp(c); + if (app != null) { + T a = instantiator.instantiate(app, cl, className, intent); if (a != null) return a; } return (T) cl.loadClass(className).newInstance(); } + private Application getApp(Context c) { + // We need this shortcut to avoid actually calling getApplicationContext() on an Application + // because the Application may not return itself for getApplicationContext() because the + // API doesn't enforce it. + if (c instanceof Application) return (Application) c; + if (c.getApplicationContext() instanceof Application) { + return (Application) c.getApplicationContext(); + } + return null; + } + private interface Instantiator<T> { T instantiate(Application app, ClassLoader cl, String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException; |
