Simple use of ARouter

Rely on

implementation 'com. Alibaba: arouter - API: 1.3.1'
annotationProcessor 'com. Alibaba: arouter - compiler: 1.1.4'
Copy the code

Android-defaultconfig is added under the android-defaultConfig node. Arouter is added as well as components

				javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
Copy the code

Initialize the

  1. Initialize the SDK in the Application
private void initARouter(a) {
    if (isDebug()) {           // These two lines must be written before init, otherwise these configurations will not be valid during init
        ARouter.openLog();     // Prints logs
        ARouter.openDebug();   // Enable debug mode (if running in InstantRun mode, debug must be enabled! The online version needs to be closed, otherwise there is a security risk.
    }
    ARouter.init(this); // As early as possible, it is recommended to initialize in Application
}

public boolean isDebug(a) {
    return debug;
}
Copy the code
  1. Confusion set
-keep public class com.alibaba.android.arouter.routes. * *{*; } -keeppublic class com.alibaba.android.arouter.facade. * *{*; } -keepclass * implements com.alibaba.android.arouter.facade.template.ISyringe{*; } -keepinterface * implements com.alibaba.android.arouter.facade.template.IProvider
Copy the code

The basic use

  1. Jump without parameters
  • To jump to the page, use build to set the path to jump to
ARouter.getInstance().build("/signing/main").navigation();
Copy the code
  • Target page, using the @route annotation to set path
@Route(path = "/signing/main")
public class SigningActivity extends AppCompatActivity {... }Copy the code
  1. With reference to jump
  • To jump to a page, use withXXX to set the parameter to be passed
ARouter.getInstance().build(ArouterConstan.SIGNING_MAIN)
        .withString("path", path)
        .navigation();
Copy the code
  • The target page

The first and second types need to match arouter.getInstance ().inject(this); Called in onCreate

// The first way is to specify the name of the variable
@Autowired(name = "path")
private String path_alias;

// The variable name must be the same as key
@Autowired(a)private String path;

/ / the third kind
private String in_path = getIntent.getExtras().getString("path");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signing);
    ARouter.getInstance().inject(this); . }Copy the code
  1. Jump with object
  • Defining the parse class
@Route(path = "/signing/json")
public class JsonSerializationService implements SerializationService {
    Gson gson;
    @Override
    public <T> T json2Object(String input, Class<T> clazz) {
        return gson.fromJson(input,clazz);
    }
    @Override
    public String object2Json(Object instance) {
        return gson.toJson(instance);
    }
    @Override
    public <T> T parseObject(String input, Type clazz) {
        return gson.fromJson(input,clazz);
    }
    @Override
    public void init(Context context) {
        gson = newGson(); }}Copy the code
  • The jump page
FileInfo fileInfo = new FileInfo();
ARouter.getInstance().build(ArouterConstan.SIGNING_MAIN)
        .withString("path", path)
        .withObject("obj",fileInfo)
        .navigation();
Copy the code
  • The target page
SerializationService serializationService = ARouter.getInstance().navigation(JsonSerializationService.class);
    serializationService.init(this);
    FileData obj = serializationService.parseObject(getIntent().getStringExtra("obj"), User.class);
Copy the code
  1. Jumping through URI
  • Configured under the Activity node of the AndroidManifest file
 <intent-filter>
        <data
        android:host="ideal.com"
        android:scheme="test"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
Copy the code
  • The jump page
Uri uri = Uri.parse("test://ideal.com/signing/main");
ARouter.getInstance().build(uri).navigation();
Copy the code
  • The target page
@Route(path = "/signing/main")
public class SigningActivity extends AppCompatActivity{... }Copy the code
  1. Jump back
ARouter.getInstance().build(uri).navigation(this.new NavCallback() {
    @Override
    public void onInterrupt(Postcard postcard) {
        super.onInterrupt(postcard);
        // Intercepted
    }

    @Override
    public void onArrival(Postcard postcard) {
        // Return control normally}});Copy the code
  1. Jump to intercept
@Interceptor(priority = 8, name = "test interceptor")
public class TestInterceptor implements IInterceptor {
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {

      // Both must be called
        callback.onContinue(postcard);  
        // callback.onInterrupt(new RuntimeException("Something exception"));      
    }

    @Override
    public void init(Context context) {}}Copy the code
  1. StartActivityForResult mode
ARouter.getInstance().build("/home/main")
  .navigation(this.5);// The first parameter must be Activity and the second parameter must be requestCode
Copy the code
  1. Page jump animation
ARouter.getInstance()
    .build("/signing/main")
    .withTransition(R.anim.activity_in, R.anim.activity_out)
    .navigation(this);
Copy the code
  1. Skip all interceptors
ARouter.getInstance().build("/signing/main").greenChannel().navigation();
Copy the code