Navigator.java 文件源码

java
阅读 40 收藏 0 点赞 0 评论 0

项目:simple-stack 作者:
/**
 * Attempt to find the Activity in the Context through the chain of its base contexts.
 *
 * @throws IllegalArgumentException if context is null
 * @throws IllegalStateException if the context's base context hierarchy doesn't contain an Activity
 *
 * @param context the context
 * @param <T> the type of the Activity
 * @return the Activity
 */
@NonNull
public static <T extends Activity> T findActivity(@NonNull Context context) {
    if(context == null) {
        throw new IllegalArgumentException("Context cannot be null!");
    }
    if(context instanceof Activity) {
        // noinspection unchecked
        return (T) context;
    } else {
        ContextWrapper contextWrapper = (ContextWrapper) context;
        Context baseContext = contextWrapper.getBaseContext();
        if(baseContext == null) {
            throw new IllegalStateException("Activity was not found as base context of view!");
        }
        return findActivity(baseContext);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号