开发者

determining if a Class object is an instance of an abstract class

I'm trying to d开发者_运维技巧etermine if a generic class object is an instance of an abstract class. So far I'm not having much luck. Below is the code I'm trying to use. AbstractActivity is the name of a parent class I extend some of my activities from.

public void startActivity(Intent intent)
{
    ComponentName name = intent.getComponent();

    if(name != null)
    {
        Class<?> cls = null;
        try {
            cls = Class.forName(name.getClassName());

            if(cls.isInstance(AbstractActivity));
            {
                //do something
            }
            else
            {
                super.startActivity(intent);
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    super.startActivity(intent);
}


I would try:

if(AbstractActivity.class.isAssignableFrom(cls)) {
    ....
}


You can base on simplename of class.

this.getClass().getSimpleName().equals(LauncherActivity.class.getSimpleName())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜