0

I want my app to be able to be run on pre Android 2.0 operating systems (namely 1.5 and 1.6). I need to include Activity.onAttachedToWindow() for 2.0 and above. How can I use reflection (or any other approach) to make my app work property on pre-2.0 Android operating systems?

user645402
  • 737
  • 1
  • 16
  • 34
  • What do you mean under "include"? Do you want to call this method or override it? – inazaruk May 17 '11 at 19:06
  • On another note, I would recommend to think twice before investing your time into 1.5/1.6 support. It's only around 5% of active users, but your code becomes significantly more harder to support. As another option, you could stick with 1.6, and leave 1.5 alone (~2.3% of users). – inazaruk May 17 '11 at 19:08
  • I mean I need to override it. It's kind of a weird case because I'm not the one calling that, so i'm not sure how reflection could play a role here... – user645402 May 18 '11 at 02:14

1 Answers1

1

Activity's onAttachedToWindow is empty. This means you can avoid calling super.onAttachedToWindow. So the easiest way would be:

@Override
public void onAttachedToWindow()
{   
     Log.e("TEST", "onAttachedToWindow");               
}

Android OS will call your onAttachedToWindow on Api Level 5+ (2.0+). And on 1.5/1.6 this function is just never called.


If you want to call implementation of onAttachedToWindow from super class via reflection:

@Override
public void onAttachedToWindow()
{   
    Log.e("TEST", "onAttachedToWindow");

    /* calling:
     * super.onAttachedToWindow(); 
     */
    Class<?> activityClass = (Class<?>)getClass().getSuperclass();
    try
    {
        Method superOnAttachedToWindow = activityClass.getMethod("onAttachedToWindow");
        superOnAttachedToWindow.invoke(this);
    }
    catch(InvocationTargetException ex)
    {
        //TODO: add exception handling
    }
    catch(IllegalAccessException ex)
    {
        //TODO: add exception handling;
    }
    catch(IllegalArgumentException ex)
    {
        //TODO: add exception handling
    }
    catch(NoSuchMethodException ex)
    {
        /* you are here if `onAttachedToWindow` does not exist */           
    }

}
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • should that be: superOnAttachedToWindow.invoke( activityClass ); otherwise i get a stack overflow... – user645402 May 23 '11 at 22:56
  • @user645402 I think changing it to `activityClass`, which means passing a `Class` as `this` to `onAttachedToWindow`, doesn't really help and you probably got an `IllegalArgumentException` which you didn't log. – TWiStErRob Dec 10 '14 at 12:45
  • @inazaruk I think he's right though, reflection doesn't help here since the documentation for `invoke` says: "*Reflective method invocation follows the usual process for method lookup.*" which means it's polymorphic, hence calling your override, not `super`, see http://stackoverflow.com/q/5411434/253468; BUT you can use reflection to check whether there's a `super.onAttachedToWindow()` and call it the normal way if no `NoSuchMethodException` was thrown. – TWiStErRob Dec 10 '14 at 12:49