An overview of the
The original project is distributed by the background URL. IntentFilter is configured in the Manifest for each Activity on the client to realize the jump and parameter transfer (implicit jump) by matching the URL. These pages also support Intent jumps, which are passed through bundles (showing jumps). A unified jump to a Fragment requires putting the relevant logic into the container Activity, causing the common Fragment container to register a bunch of IntentFilters and parsing parameters for each Fragment individually. With the iteration of the project, more and more pages support skipping, more and more complex parameter transmission, and more and more bloated Manifest. The Activity also needs to parse URL and bundle to obtain parameters by judging intent, which increases a lot of maintenance costs for the iteration and maintenance of the project. Based on the above problems, I investigated several mainstream open source routing frameworks and found that none of them could fully meet my requirements. Some of them did not support configuring multipathing (some pages must support multipathing for historical reasons). Some do not support automatic parameter resolution (convenience is greatly discounted); General function, jump logic complex; Most of them do not support direct jump of fragments, and kolin support is uneven. After referring to a number of routing frameworks, I implemented a lightweight Android routing framework: PageRouter, perfect solution to the above pain points, the following is PageRouter’s related features and use documents.
Github
Github.com/liujingg/Pa…
The effect
The characteristics of
- Supports Activity standard URL resolution and jump
- Supports fragments to jump directly to the specified associated Activity
- Multipathing
- Supports multi-module projects
- Support the kotlin
- Support automatic parameter injection, kotlin support attribute delegate to obtain parameters
- Support for adding interceptors
- Supports global and local degrade policies
Add the dependent
android { defaultConfig { ... //multi module configuration javaCompileOptions { annotationProcessorOptions { arguments = [targetModuleName: 'Other']// replace with the other module project name } } } } dependencies { // Replace the last version implementation "com.liujing.pagerouter:router:last-version" annotationProcessor "com.liujing.pagerouter:router-compiler:last-version" . }Copy the code
The kotlin project replaces the annotationProcessor with kapt
apply plugin: 'kotlin-kapt
// Replace the last version
implementation "com.liujing.pagerouter:router:last-version"
kapt "com.liujing.pagerouter:router-compiler:last-version"Copy the code
Function and Use
1. Initialization
Initialization in Application is strongly recommended.
Router.init("pagerouter"); //your application's specific schemeCopy the code
Register other module routes (if any).
Router.register(new OtherRouterInitializer());Copy the code
Manually add a route entry.
Router.register(new RouterInitializer() {
@Override
public void initActivityTable(Map<String, Class<? extends Activity>> router) {
router.put("second2", SecondActivity.class);
}
@Override
public void initFragmentTable(Map<String, Pair<Class<? extends Activity>, Class<? extends Fragment>>> router) {
}
});Copy the code
2. Add annotations
The Activity annotation
@RouterActivity({"second", "third"})
public class SecondActivity extends AppCompatActivity {
...
}Copy the code
Fragments annotations
// The activityClazz here means the fragment currently associated with Activity
@RouterFragment(value = "myfragment", activityClazz = FragmentContainerActivity.class)
public class MyFragment extends Fragment {
...
}Copy the code
3. Parameter analysis
@RouterActivity({"second"}) public class SecondActivity extends AppCompatActivity { @RouterField("id") // map parameters in the url by name private int id; . @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_common); Router.inject(this); // PageRouter will automatically set value of fields ... }}Copy the code
4. Initiate the forward
Router.startActivity(context,"scheme://second? id=17")Copy the code
5. Confused
-keep class * extends com.liujing.pagerouter.RouterInitializer { *; }Copy the code
6. Jump result callback
Router.startActivity( this, "pagerouter://other? id=17", object : RouteCallback { override fun onSuccess(context: Context, uri: Uri) { Toast.makeText(context, "success", Toast.LENGTH_SHORT).show() } override fun onFailed(context: Context? , message: String?) { Toast.makeText(context, "failed : $message", Toast.LENGTH_SHORT).show() } })Copy the code
7. External URL redirect
androidmanifest.xml
<activity android:name=".RouterCenterActivity">
<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="myscheme"/>
</intent-filter>
</activity>Copy the code
Create a new Activity to listen for Scheme events
public class RouterCenterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri data = getIntent().getData();
if (data != null) {
Router.startActivity(this, data.toString());
}
this.finish();
}
}Copy the code
8. Add interceptors
Router.setIntercept(new IIntercept() { @Override public void process(@NonNull Context context, @NonNull Uri uri, InterceptorCallback callback) { if (...) { //TODO do something callback.onInterrupt(result, message); //interrupt routing process } else { callback.onContinue(uri); }}});Copy the code
9. Global policies
Router.setDefaultCallBack(new RouteCallback() {
@Override
public void onSuccess(Context context, Uri uri) {
}
@Override
public void onFailed(Context context, String message) {
//TODO do something
}
});Copy the code