Reputation: 837
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?
Upvotes: 0
Views: 1574
Reputation: 74790
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 */
}
}
Upvotes: 1