ActivityRouter

function

Support for defining urls for activities, so that you can jump to the Activity from the URL, and support jumping in the browser and app.

integration

The root directory build. Gradle

buildscript {
  dependencies {
    classpath 'com. Neenbedankt. Gradle. Plugins: android - apt: 1.7'
  }
}Copy the code

Project app/build. Gradle

apply plugin: 'android-apt'

dependencies {
    compile 'com. Making. Mzule. Activityrouter: activityrouter: 1.1.1'
    apt 'com. Making. Mzule. Activityrouter: the compiler: 1.1.1'
}
Copy the code

In the AndroidManifest. XML configuration

<activity
    android:name="com.github.mzule.activityrouter.router.RouterActivity"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="mzule" /><! -- Change your own scheme-->
    </intent-filter>
</activity>Copy the code

Add annotations to the Activity you want to configure

@Router("main")
public class MainActivity extends Activity {. }Copy the code

This opens MainActivity with mzule://main.

The advanced

Multiple IP addresses can be configured

@Router({"main"."root"})Copy the code

Mzule :// Main and mzule://root can both access the same Activity

Support to obtain the URL?Passed parameters

@Router("main")Copy the code

The above configuration can be done via mzule://main? MainActivity#onCreate = getIntent().getStringExtra(“name”) All parameters default to String, but you can specify parameter types through configuration, as described below.

Support for defining parameters in path

@Router("main/:color")Copy the code

Mzule ://main/0xff878798, The color value 0xFF878798 can be obtained from MainActivity#onCreate via getIntent().getStringExtra(“color”)

Multi-level path parameters are supported

@Router("user/:userId/:topicId/:commentId")

@Router("user/:userId/topic/:topicId/comment/:commentId")Copy the code

The above two methods are supported and three parameters are defined: userId,topicId, and commentId

Parameter types can be specified

@Router(value = "main/:color", intExtra = "color")Copy the code

This specifies that the color parameter is of type int. In MainActivity#onCreate the color can be obtained by getIntent().getinTextra (“color”, 0). Support parameter types have int, long, short, byte, char, and float, double, Boolean, do not specify a default String type.

Preferential adaptation

@Router("user/:userId")
public class UserActivity extends Activity {. }@Router("user/statistics")
public class UserStatisticsActivity extends Activity {. }Copy the code

Given the above two configurations,

If preferential adaptation is not supported, mzule://user/statistics may be matched to @router (“user/:userId”) with userId=statistics

Mzule ://user/statistics will be matched to @router (“user/statistics”) and not to the previous @router (“user/:userId”).

Support the Callback

public class App extends Application implements RouterCallbackProvider {
    @Override
    public RouterCallback provideRouterCallback() {
        return new SimpleRouterCallback() {
            @Override
            public void beforeOpen(Context context, Uri uri) {
                context.startActivity(new Intent(context, LaunchActivity.class));
            }

            @Override
            public void afterOpen(Context context, Uri uri) {
            }

            @Override
            public void notFound(Context context, Uri uri) {
                context.startActivity(newIntent(context, NotFoundActivity.class)); }}; }}Copy the code

The Application implements the RouterCallbackProvider interface and provides the RouterCallback through the provideRouterCallback() method.

Supports Http(S)

@Router({"http://mzule.com/main"."main"})Copy the code

AndroidManifest.xml

<activity
    android:name="com.github.mzule.activityrouter.router.RouterActivity"
    android:theme="@android:style/Theme.NoDisplay">
    .
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="mzule.com" />
    </intent-filter>
</activity>Copy the code

In this way, http://mzule.com/main and mzule://main can both be mapped to the same Activity. It is worth noting that the @router declaration of the HTTP protocol address requires the full name.

Support parameter transfer

@Router(value = "item", longExtra = "id", transfer = "id=>itemId")Copy the code

Transfer = “id=>itemId”; transfer = “id=>itemId”; transfer = “id=>itemId”; transfer = “id=>itemId” Note that either longExtra = “id” or longExtra = “itemId” can be set to long.

Support for in-application calls

Routers.open(context."mzule://main/0xff878798")
Routers.open(context, Uri.parse("mzule://main/0xff878798"))Copy the code

Open (Context, String) or Routers. Open (Context, Uri), you can directly open an Activity within an application, and do not need to go through a RouterActivity, for higher efficiency.

Confuse configuration

-keep class com.github.mzule.activityrouter.router{. * * *; }Copy the code