The most lightweight Framework about router in android.
module | GoRouter-api | GoRouter-compiler | GoRouter-annotation |
---|---|---|---|
version |
- Support for multi-modules
- Full Activity support
- Support get Fragment instance
- InstantRun support
- MultiDex support
- AndroidX Support
- Support kotlin
config.gradle
dependencies{
//annotation
implementation 'cn.rubintry:gorouter-annotation:1.0.3'
//core api
implementation 'cn.rubintry:gorouter-api:1.0.33'
//Java
annotationProcessor 'cn.rubintry:gorouter-compiler:1.0.5'
//Kotlin
kapt 'cn.rubintry:gorouter-compiler:1.0.5'
}
gradle.properties
//If you need run sub module as Application, you should make moduleIsApplication true
moduleIsApplication = false
mainModule.gradle
apply plugin: 'com.android.application'
apply from: '../config.gradle'
dependencies {
if(!moduleIsApplication.toBoolean().booleanValue()){
implementation project(path: ':anothermodule')
}
}
anotherModule.gradle
if(moduleIsApplication.toBoolean().booleanValue()){
apply plugin: 'com.android.application'
}else{
apply plugin: 'com.android.library'
}
apply from: '../config.gradle'
android {
defaultConfig {
if(moduleIsApplication.toBoolean().booleanValue()){
applicationId "Your sub module's package name"
}
...
}
sourceSets{
main {
if(moduleIsApplication.toBoolean().booleanValue()){
//If you run this module as application , you can use the default AndroidManifest.xml
manifest.srcFile 'src/main/AndroidManifest.xml'
}else{
//If you run this module as library , you should create a new AndroidManifest liked follows
manifest.srcFile 'src/main/manifest/AndroidManifest.xml'
}
}
}
}
The AndroidManifest.xml when you run module as library
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package name">
<application>
<activity android:name="your activity in another module">
</activity>
</application>
</manifest>
Fragment
//Get a fragment instance
Fragment instance = GoRouter.getInstance().build("fragmentRouteKey").go()
Activity
//Navigation to an Activity immediately
GoRouter.getInstance().build("routeKey2").go()
//With data
Bundle data = new Bundle()
data.putInt(key , value);
GoRouter.getInstance().build("routeKey2" , data).go()
Target Activity
/**
* @author logcat
*/
@Route(url = "routeKey2")
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
value = getIntent().getInt(key)
}
}
Activity
activity1
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:transitionName="sharedActivity"
android:textSize="50sp"></TextView>
activity2
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:transitionName="sharedActivity"
android:textSize="50sp"></TextView>
//Activity1
//navigation to another activity with animation.
tv = findViewById(R.id.tv);
GoRouter.getInstance().build("activity1 's routeKey")
.go(this , ActivityOptionsCompat.makeSceneTransitionAnimation(this , tv , tv.getTransitionName()).toBundle())
Decoupled by exposure service
/**
* Expose this service in common lib.
*
*/
interface SimpleService implements IProvider{
void simple(Context context);
}
/**
* Implements this service in module B and deal with it.
*
*/
@Route(url = "Simple/SimpleService")
public class SimpleServiceImpl implements SimpleService{
@override
public void init(Context context){
//Here you can do something to initialize.
}
public void simple(Context context){
//do something
}
}
/**
* An activity in module A
*
*/
public class SimpleActivity extends AppCompatActivity{
@ovserride
public void oncreate(Bundle savedInstanceState){
SimpleService service = GoRouter.getInstance().build("Simple/SimpleService").go(SimpleService.class);
if(service != null){
service.simple(this.getApplicationContext());
}
}
}