AriArouter demo 학습(하나)

9914 단어 android
1.https://github.com/alibaba/ARouter    
Arouter는 프로젝트의 구성화를 위해 프로젝트 수요에 따라 서로 다른 업무 모듈을 구분하고 서로 다른 업무 모듈 간의 결합을 해소하며 영향을 주지 않고 상호작용을 하기 위해 만들어진 것이다. 프로젝트 개발에서 모듈 개발자는 자신의 업무 모듈만 컴파일하여 테스트할 수 있고 시간을 절약하고 효율을 높일 수 있다.
2. MainActivity와 같은 인터페이스 점프 코드 사례
package com.alibaba.android.arouter.demo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testservice.HelloService;
import com.alibaba.android.arouter.demo.testservice.SingleService;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity = this;
    }

    public static Activity getThis() {
        return activity;
    }

    /**
     * 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.kotlinNavigation:

//                kotlin  
                ARouter.getInstance()
                        .build("/kotlin/test")
                        .withString("name", "  ")
                        .withInt("age", 23)
                        .navigation();
                break;

            case R.id.normalNavigation2:
                //    forResult         
                ARouter.getInstance()
                        .build("/test/activity2")
                        .navigation(this, 666);
                break;

            case R.id.getFragment:
                //  fragment   
                Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment").navigation();
                Toast.makeText(this, "  Fragment:" + fragment.toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.normalNavigationWithParams:
                // ARouter.getInstance()
                //         .build("/test/activity2")
                //         .withString("key1", "value1")
                //         .navigation();
                //     
                Uri testUriMix = Uri.parse("arouter://m.aliyun.com/test/activity2");
                ARouter.getInstance().build(testUriMix)
                        .withString("key1", "value1")
                        .navigation();

                break;
            case R.id.oldVersionAnim:
                //         
                ARouter.getInstance()
                        .build("/test/activity2")
                        .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
                        .navigation(this);
                break;
            case R.id.newVersionAnim:

                //         

                if (Build.VERSION.SDK_INT >= 16) {
                    ActivityOptionsCompat compat = ActivityOptionsCompat.
                            makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);

                    ARouter.getInstance()
                            .build("/test/activity2")
                            .withOptionsCompat(compat)
                            .navigation();
                } else {
                    Toast.makeText(this, "API < 16,        ", Toast.LENGTH_SHORT).show();
                }



                break;
            case R.id.interceptor:
                //     
                ARouter.getInstance()
                        .build("/test/activity4")
                        .navigation(this, new NavCallback() {
                            @Override
                            public void onArrival(Postcard postcard) {

                            }

                            @Override
                            public void onInterrupt(Postcard postcard) {
                                Log.d("ARouter", "    ");
                            }
                        });
                break;
            case R.id.navByUrl:
                //  url   
                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);
                List objList = new ArrayList<>();
                objList.add(testObj);

                Map> map = new HashMap<>();
                map.put("testMap", objList);

                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)
                        .withObject("objList", objList)
                        .withObject("map", map)
                        .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:
                //    moudle1  module-java    
                ARouter.getInstance().build("/module/1").navigation();
                break;

            case R.id.navToMoudle2:
                //          Group      moudle2
                ARouter.getInstance().build("/module/2", "m2").navigation();
                break;

            case R.id.callSingle:
                 //       
                ARouter.getInstance().navigation(SingleService.class).sayHello("Mike");
                break;

            case R.id.destroy:
                ARouter.getInstance().destroy();


                break;
            case R.id.failNav:

                ARouter.getInstance().build("/xxx/xxx").navigation(this, new NavCallback() {
                    @Override
                    public void onFound(Postcard postcard) {
                        Log.d("ARouter", "   ");
                    }

                    @Override
                    public void onLost(Postcard postcard) {
                        Log.d("ARouter", "    ");
                    }

                    @Override
                    public void onArrival(Postcard postcard) {
                        Log.d("ARouter", "    ");
                    }

                    @Override
                    public void onInterrupt(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;


            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 666:
                Log.e("activityResult", String.valueOf(resultCode));
                break;
            default:
                break;
        }
    }
}

3. 프로젝트에서 아직 쓸모가 없어서 다른 사람이 밟은 구덩이를 봤다.
https://blog.csdn.net/u013762572/article/details/77150769

좋은 웹페이지 즐겨찾기