Search in sources :

Example 1 with Postcard

use of com.alibaba.android.arouter.facade.Postcard in project ARouter by alibaba.

the class SchemeFilterActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //        直接通过ARouter处理外部Uri
    Uri uri = getIntent().getData();
    ARouter.getInstance().build(uri).navigation(this, new NavigationCallback() {

        @Override
        public void onFound(Postcard postcard) {
            finish();
        }

        @Override
        public void onLost(Postcard postcard) {
            finish();
        }
    });
}
Also used : Postcard(com.alibaba.android.arouter.facade.Postcard) NavigationCallback(com.alibaba.android.arouter.facade.callback.NavigationCallback) Uri(android.net.Uri)

Example 2 with Postcard

use of com.alibaba.android.arouter.facade.Postcard in project ARouter by alibaba.

the class MainActivity method onClick.

/**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.openLog:
            ARouter.openLog();
            break;
        case R.id.openDebug:
            ARouter.openDebug();
            break;
        case R.id.init:
            // 调试模式不是必须开启,但是为了防止有用户开启了InstantRun,但是
            // 忘了开调试模式,导致无法使用Demo,如果使用了InstantRun,必须在
            // 初始化之前开启调试模式,但是上线前需要关闭,InstantRun仅用于开
            // 发阶段,线上开启调试模式有安全风险,可以使用BuildConfig.DEBUG
            // 来区分环境
            ARouter.openDebug();
            ARouter.init(getApplication());
            break;
        case R.id.normalNavigation:
            ARouter.getInstance().build("/test/activity2").navigation();
            break;
        case R.id.normalNavigationWithParams:
            ARouter.getInstance().build("/test/activity2").withString("key1", "value1").navigation();
            break;
        case R.id.interceptor:
            ARouter.getInstance().build("/test/activity4").navigation();
            break;
        case R.id.navByUrl:
            ARouter.getInstance().build("/test/webview").withString("url", "file:///android_asset/schame-test.html").navigation();
            break;
        case R.id.autoInject:
            TestParcelable testParcelable = new TestParcelable("jack", 666);
            TestObj testObj = new TestObj("Rose", 777);
            ARouter.getInstance().build("/test/activity1").withString("name", "老王").withInt("age", 18).withBoolean("boy", true).withLong("high", 180).withString("url", "https://a.b.c").withParcelable("pac", testParcelable).withObject("obj", testObj).navigation();
            break;
        case R.id.navByName:
            ((HelloService) ARouter.getInstance().build("/service/hello").navigation()).sayHello("mike");
            break;
        case R.id.navByType:
            ARouter.getInstance().navigation(HelloService.class).sayHello("mike");
            break;
        case R.id.navToMoudle1:
            ARouter.getInstance().build("/module/1").navigation();
            break;
        case R.id.navToMoudle2:
            // 这个页面主动指定了Group名
            ARouter.getInstance().build("/module/2", "m2").navigation();
            break;
        case R.id.destroy:
            ARouter.getInstance().destroy();
            break;
        case R.id.failNav:
            ARouter.getInstance().build("/xxx/xxx").navigation(this, new NavigationCallback() {

                @Override
                public void onFound(Postcard postcard) {
                }

                @Override
                public void onLost(Postcard postcard) {
                    Log.d("ARouter", "找不到了");
                }
            });
            break;
        case R.id.failNav2:
            ARouter.getInstance().build("/xxx/xxx").navigation();
            break;
        case R.id.failNav3:
            ARouter.getInstance().navigation(MainActivity.class);
            break;
        case R.id.normalNavigation2:
            ARouter.getInstance().build("/test/activity2").navigation(this, 666);
            break;
        default:
            break;
    }
}
Also used : Postcard(com.alibaba.android.arouter.facade.Postcard) NavigationCallback(com.alibaba.android.arouter.facade.callback.NavigationCallback) HelloService(com.alibaba.android.arouter.demo.testservice.HelloService) TestParcelable(com.alibaba.android.arouter.demo.testinject.TestParcelable) TestObj(com.alibaba.android.arouter.demo.testinject.TestObj)

Example 3 with Postcard

use of com.alibaba.android.arouter.facade.Postcard in project ARouter by alibaba.

the class InterceptorServiceImpl method _excute.

/**
     * Excute interceptor
     *
     * @param index    current interceptor index
     * @param counter  interceptor counter
     * @param postcard routeMeta
     */
private static void _excute(final int index, final CancelableCountDownLatch counter, final Postcard postcard) {
    if (index < Warehouse.interceptors.size()) {
        IInterceptor iInterceptor = Warehouse.interceptors.get(index);
        iInterceptor.process(postcard, new InterceptorCallback() {

            @Override
            public void onContinue(Postcard postcard) {
                // Last interceptor excute over with no exception.
                counter.countDown();
                // When counter is down, it will be execute continue ,but index bigger than interceptors size, then U know.
                _excute(index + 1, counter, postcard);
            }

            @Override
            public void onInterrupt(Throwable exception) {
                // Last interceptor excute over with fatal exception.
                // save the exception message for backup.
                postcard.setTag(null == exception ? new HandlerException("No message.") : exception.getMessage());
                counter.cancel();
            // Be attention, maybe the thread in callback has been changed,
            // then the catch block(L207) will be invalid.
            // The worst is the thread changed to main thread, then the app will be crash, if you throw this exception!
            //                    if (!Looper.getMainLooper().equals(Looper.myLooper())) {    // You shouldn't throw the exception if the thread is main thread.
            //                        throw new HandlerException(exception.getMessage());
            //                    }
            }
        });
    }
}
Also used : Postcard(com.alibaba.android.arouter.facade.Postcard) HandlerException(com.alibaba.android.arouter.exception.HandlerException) IInterceptor(com.alibaba.android.arouter.facade.template.IInterceptor) InterceptorCallback(com.alibaba.android.arouter.facade.callback.InterceptorCallback)

Example 4 with Postcard

use of com.alibaba.android.arouter.facade.Postcard in project ARouter by alibaba.

the class _ARouter method navigation.

protected <T> T navigation(Class<? extends T> service) {
    try {
        Postcard postcard = LogisticsCenter.buildProvider(service.getSimpleName());
        LogisticsCenter.completion(postcard);
        return (T) postcard.getProvider();
    } catch (NoRouteFoundException ex) {
        logger.warning(Consts.TAG, ex.getMessage());
        return null;
    }
}
Also used : Postcard(com.alibaba.android.arouter.facade.Postcard) NoRouteFoundException(com.alibaba.android.arouter.exception.NoRouteFoundException)

Aggregations

Postcard (com.alibaba.android.arouter.facade.Postcard)4 NavigationCallback (com.alibaba.android.arouter.facade.callback.NavigationCallback)2 Uri (android.net.Uri)1 TestObj (com.alibaba.android.arouter.demo.testinject.TestObj)1 TestParcelable (com.alibaba.android.arouter.demo.testinject.TestParcelable)1 HelloService (com.alibaba.android.arouter.demo.testservice.HelloService)1 HandlerException (com.alibaba.android.arouter.exception.HandlerException)1 NoRouteFoundException (com.alibaba.android.arouter.exception.NoRouteFoundException)1 InterceptorCallback (com.alibaba.android.arouter.facade.callback.InterceptorCallback)1 IInterceptor (com.alibaba.android.arouter.facade.template.IInterceptor)1