Search in sources :

Example 1 with AfIntent

use of com.andframe.feature.AfIntent in project AndFrameWorks by scwang90.

the class AfActivity method onCreate.

// </editor-fold>
// <editor-fold desc="生命周期">
/**
 * final 原始 onCreated(Bundle bundle)
 * 子类只能重写 onCreated(Bundle bundle,AfIntent intent)
 */
@Override
protected void onCreate(Bundle bundle) {
    try {
        $.pager().onActivityCreated(this);
        if (AfStackTrace.isLoopCall()) {
            // System.out.println("递归检测");
            super.onCreate(bundle);
            return;
        }
        MustLogin must = AfReflecter.getAnnotation(getClass(), AfActivity.class, MustLogin.class);
        if (must != null && !AfApp.get().isUserLogined()) {
            if (Activity.class.isAssignableFrom(must.value())) {
                startActivity(new Intent(this, must.value()));
            } else if (Fragment.class.isAssignableFrom(must.value())) {
                // noinspection unchecked
                startFragment((Class<? extends Fragment>) must.value());
            }
            makeToastShort(must.remark());
            super.onCreate(bundle);
            finish();
            return;
        }
        Injecter.doInject(this);
        LayoutBinder.doBind(this);
    } catch (final Throwable e) {
        // handle 可能会根据 Activity 弹窗提示错误信息
        // 当前 Activity 即将关闭,提示窗口也会关闭
        // 用定时器 等到原始 Activity 再提示弹窗
        AfDispatcher.dispatch(() -> AfExceptionHandler.handle(e, TAG() + ".onCreated"), 500);
        super.onCreate(bundle);
        makeToastShort("页面启动失败", e);
        this.finish();
    }
    try {
        this.onCreated(bundle);
        LifeCycleInjecter.injectOnCreate(this, bundle);
    } catch (Exception e) {
        AfDispatcher.dispatch(() -> AfExceptionHandler.handle(e, TAG() + ".onCreated"), 500);
        makeToastShort("页面启动失败", e);
        this.finish();
    }
}
Also used : MustLogin(com.andframe.annotation.MustLogin) Intent(android.content.Intent) AfIntent(com.andframe.feature.AfIntent) Fragment(android.support.v4.app.Fragment) AfFragment(com.andframe.fragment.AfFragment)

Example 2 with AfIntent

use of com.andframe.feature.AfIntent in project AndFrameWorks by scwang90.

the class Injecter method injectExtra.

private static void injectExtra(Field field, Object handler, Context context) {
    InjectExtraInvalid invalid = AfReflecter.getAnnotation(handler.getClass(), InjectExtraInvalid.class);
    if (invalid != null && invalid.value().length == 0) {
        return;
    }
    if (field.isAnnotationPresent(InjectExtra.class)) {
        InjectExtra inject = field.getAnnotation(InjectExtra.class);
        if (invalid != null) {
            boolean find = false;
            for (String extra : invalid.value()) {
                if (TextUtils.equals(extra, inject.value())) {
                    find = true;
                    break;
                }
            }
            if (find) {
                return;
            }
        }
        try {
            Extrater intent = new AfIntent();
            if (handler instanceof Activity) {
                intent = new AfIntent(((Activity) handler).getIntent());
            } else if (handler instanceof Fragment) {
                final Fragment fragment = (Fragment) handler;
                Bundle bundle = fragment.getArguments();
                if (bundle != null) {
                    intent = new AfBundle(fragment.getArguments()) {

                        @Override
                        public <T> T get(String _key, Class<T> clazz) {
                            T t = super.get(_key, clazz);
                            if (t == null && fragment.getActivity() != null) {
                                return new AfIntent(fragment.getActivity().getIntent()).get(_key, clazz);
                            }
                            return t;
                        }

                        @Override
                        public <T> List<T> getList(String _key, Class<T> clazz) {
                            List<T> list = super.getList(_key, clazz);
                            if (list == null || list.size() == 0) {
                                return new AfIntent(fragment.getActivity().getIntent()).getList(_key, clazz);
                            }
                            return list;
                        }
                    };
                } else {
                    if (context instanceof Activity && fragment.getActivity() == null) {
                        intent = new AfIntent(((Activity) context).getIntent());
                    } else {
                        intent = new AfIntent(fragment.getActivity().getIntent());
                    }
                }
            }
            Object value;
            Class<?> type = field.getType();
            Type generic = field.getGenericType();
            if (type.isArray()) {
                type = type.getComponentType();
                List<?> list = intent.getList(inject.value(), type);
                value = Array.newInstance(type, list.size());
                value = list.toArray((Object[]) value);
            } else if (List.class.equals(type)) {
                ParameterizedType parameterized = (ParameterizedType) generic;
                type = (Class<?>) parameterized.getActualTypeArguments()[0];
                value = intent.getList(inject.value(), type);
            } else {
                value = intent.get(inject.value(), type);
            }
            if (value != null) {
                field.setAccessible(true);
                field.set(handler, value);
            } else if (inject.necessary()) {
                if (inject.remark().length() > 0) {
                    throw new AfException("缺少必须参数:" + inject.remark());
                } else {
                    throw new AfException("缺少必须参数:" + inject.value());
                }
            }
        } catch (AfException e) {
            throw e;
        } catch (Throwable e) {
            if (inject.necessary()) {
                throw new RuntimeException("缺少必须参数", e);
            }
            AfExceptionHandler.handle(e, TAG(handler, "doInject.InjectExtra.") + field.getName());
        }
    }
}
Also used : Bundle(android.os.Bundle) AfBundle(com.andframe.feature.AfBundle) AfActivity(com.andframe.activity.AfActivity) Activity(android.app.Activity) AfBundle(com.andframe.feature.AfBundle) Fragment(android.support.v4.app.Fragment) InjectExtra(com.andframe.annotation.inject.InjectExtra) ParameterizedType(java.lang.reflect.ParameterizedType) Extrater(com.andframe.api.Extrater) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InjectExtraInvalid(com.andframe.annotation.inject.InjectExtraInvalid) ColorStateList(android.content.res.ColorStateList) List(java.util.List) AfException(com.andframe.exception.AfException) AfIntent(com.andframe.feature.AfIntent)

Example 3 with AfIntent

use of com.andframe.feature.AfIntent in project AndFrameWorks by scwang90.

the class AfPagerManager method startActivity.

@Override
public void startActivity(Class<? extends Activity> clazz, Object... args) {
    AfActivity activity = currentActivity();
    if (activity != null && activity.isRecycled()) {
        activity.startActivity(clazz, args);
    } else {
        AfApp app = AfApp.get();
        AfIntent intent = new AfIntent(app, clazz);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putKeyVaules(args);
        app.startActivity(intent);
    }
}
Also used : AfActivity(com.andframe.activity.AfActivity) AfApp(com.andframe.application.AfApp) AfIntent(com.andframe.feature.AfIntent)

Aggregations

AfIntent (com.andframe.feature.AfIntent)3 Fragment (android.support.v4.app.Fragment)2 AfActivity (com.andframe.activity.AfActivity)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 ColorStateList (android.content.res.ColorStateList)1 Bundle (android.os.Bundle)1 MustLogin (com.andframe.annotation.MustLogin)1 InjectExtra (com.andframe.annotation.inject.InjectExtra)1 InjectExtraInvalid (com.andframe.annotation.inject.InjectExtraInvalid)1 Extrater (com.andframe.api.Extrater)1 AfApp (com.andframe.application.AfApp)1 AfException (com.andframe.exception.AfException)1 AfBundle (com.andframe.feature.AfBundle)1 AfFragment (com.andframe.fragment.AfFragment)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 List (java.util.List)1