开发者

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

开发者 https://www.devze.com 2023-01-31 07:43 出处:网络
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 nam

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

精彩评论

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