Android Router路由框架
Route路由框架的介绍:
其实,安卓系统已经给我们介绍过api跳转的 startactivity,有很多人就该问,为什么还要用router路由框架,在这里我要告诉大家,路由框架的一些优点:
1.router框架的灵活性更强,适用于复杂的业务场景,就比如一些活动,之前我们不知道活动的目的,但如果做了一些约定,我们就可以通过映射,对这个活动页面进行自由配置。
2.随着业务量的增长,客户端必然随之膨胀,开发人员的工作量越来越大,比如64K问题,比如协作开发问题。App一般都会走向组件化、插件化的道路,
而组件化、插件化的前提就是解耦,那么我们首先要做的就是解耦页面之间的依赖关系。
3.简化代码,一行代码就可以搞定。
代码如下:
Bean
import android.os.Parcel; import android.os.Parcelable; public class Bean implements Parcelable{ String name; public String age; public Bean(String name,String age){ this.name=name; this.age=age; } protected Bean(Parcel in) { name = in.readString(); age = in.readString(); } public static final Creator<Bean> CREATOR = new Creator<Bean>() { @Override public Bean createFromParcel(Parcel in) { return new Bean(in); } @Override public Bean[] newArray(int size) { return new Bean[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(name); parcel.writeString(age); } }Bactivity
import android.app.Activity; import android.os.Bundle; import android.os.PersistableBundle; import android.widget.Toast; import com.alibaba.android.arouter.facade.annotation.Autowired; import com.alibaba.android.arouter.facade.annotation.Route; import com.alibaba.android.arouter.launcher.ARouter; @Route(path = "/nihao/hehe/bctivity") public class Bactivity extends Activity{ @Autowired public String name; @Autowired public boolean islogin; @Autowired public int version; @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_b); ARouter.getInstance().inject(this); Toast.makeText(this, "传过来的数据是"+name+"--" +islogin+"--"+version,Toast.LENGTH_SHORT).show(); } }Cactivity
import android.app.Activity; import android.os.Bundle; import android.os.PersistableBundle; import android.util.Log; import com.alibaba.android.arouter.facade.annotation.Autowired; import com.alibaba.android.arouter.facade.annotation.Route; import com.alibaba.android.arouter.launcher.ARouter; import bean.Bean; @Route(path = "/nihao/hehe/heihei") public class Cactivity extends Activity{ private static final String TAG = "CActivity"; @Autowired public Bean bean; @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_c); ARouter.getInstance().inject(this); Log.e(TAG, "onCreate: "+bean==null?"bean为空":"bean"+bean.age); } }MainActivity
import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityOptionsCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.alibaba.android.arouter.launcher.ARouter; import bean.Bean; public class MainActivity extends AppCompatActivity { Button to_c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); to_c = (Button) findViewById(R.id.to_c); } public void to_b(View view){ ARouter.getInstance().build(RouterHelper.toB) .withString("name","nihao") .withBoolean("islogin",false) .withInt("version",1) .withTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right) .navigation(this); } @RequiresApi(api= Build.VERSION_CODES.JELLY_BEAN) public void to_c(View view){ ActivityOptionsCompat activityOptionsCompat=ActivityOptionsCompat .makeCustomAnimation(this,android.R.anim.slide_in_left,android.R.anim.slide_out_right); ARouter.getInstance().build(RouterHelper.toC) .withParcelable("bean",new Bean("nihao","hehe")) .withOptionsCompat(activityOptionsCompat) .navigation(this); } }MyAplication
import android.app.Application; import android.graphics.Bitmap; import com.alibaba.android.arouter.launcher.ARouter; public class MyAplication extends Application{ private boolean debug=true; @Override public void onCreate() { super.onCreate(); if(isDebug()){ /* * 这两行必须写在int之前,否则这些将无效 * */ ARouter.openLog(); ARouter.openDebug();//如果在instentRun模式下运行,必须开启调式模式,线上版本需要调试,否则有安全风险 } //把router舒适化 ARouter.init(this); } public boolean isDebug(){ return debug; } }RouterHelper
public class RouterHelper { public static String toB="/nihao/hehe/bctivity"; public static String toC="/nihao/hehe/heihei"; }