Android ARouter路由中传对象遇到的坑

在ARouter的github上,最初还以为是实现SerializationService这个类,其实不是。是单独写一个类实现它,代码如下:

/**
 * Author: meizivskai
 * Time: 2020-01-01
 * Description: This is JsonServiceImpl
 */
@Route(path = "/service/json")
public class JsonServiceImpl implements SerializationService {

    @Override
    public void init(Context context) {

    }

    @Override
    public  T json2Object(String text, Class clazz) {
        return JSON.parseObject(text, clazz);
    }

    @Override
    public String object2Json(Object instance) {
        return JSON.toJSONString(instance);
    }

    @Override
    public  T parseObject(String input, Type clazz) {
        return JSON.parseObject(input, clazz);
    }
}

自定义的实体,不能序列化,否则取不到值。我的自定义对象:

/**
 * Author: meizivskai
 * Time: 2019-12-12
 * Description: This is TrueFalse
 */
public class TrueFalse {
    public int mQuestion;

    public boolean mTrueQuestion;

    public TrueFalse() {
    }

    public TrueFalse(int question, boolean trueQuestion) {
        this.mQuestion = question;
        this.mTrueQuestion = trueQuestion;
    }

    public int getmQuestion() {
        return mQuestion;
    }

    public TrueFalse setmQuestion(int mQuestion) {
        this.mQuestion = mQuestion;
        return this;
    }

    public boolean ismTrueQuestion() {
        return mTrueQuestion;
    }

    public TrueFalse setmTrueQuestion(boolean mTrueQuestion) {
        this.mTrueQuestion = mTrueQuestion;
        return this;
    }

    @Override
    public String toString() {
        return "TrueFalse{" +
                "mQuestion=" + mQuestion +
                ", mTrueQuestion=" + mTrueQuestion +
                '}';
    }
}

需要注意的是实体中,要写无参的构造方法,属性是公开权限public。

点击跳转:

mFalseButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final TrueFalse trueFalse = new TrueFalse(100, false);
        ARouter.getInstance().build(Constant.PATH_SECOND)
                .withObject("trueFalse", trueFalse)
                .navigation(MainActivity.this, new NavigationCallback() {
                    @Override
                    public void onFound(Postcard postcard) {
                        Log.e("router", "onFound: " + postcard.getPath());
                    }

                    @Override
                    public void onLost(Postcard postcard) {
                        Log.e("router", "onLost: " + postcard.getPath());
                    }

                    @Override
                    public void onArrival(Postcard postcard) {
                        Log.e("router", "onArrival: " + postcard.getPath());
                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {
                        Log.e("router", "onInterrupt: " + postcard.getPath());
                    }
                });
    }
});

界面接受值:

/**
 * Author: meizivskai
 * Time: 2020-01-01
 * Description: This is FirstActivity
 */
@Route(path = Constant.PATH_SECOND)
public class SecondActivity extends BaseActivity {

    @Autowired
    TrueFalse trueFalse;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.e("router", "onCreate: " + trueFalse);
    }
}

BaseActivity里面要注入下:

/**
 * Author: meizivskai
 * Time: 2020-01-01
 * Description: This is BaseActivity
 */
public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

    }
}

结果就可以取到值了:

 

终结下:

1.实体中成员变量最好写成public

2.实体要写无参的构造方法

3.实体不能被序列化

4.写一个实现了SerializationService的类

你可能感兴趣的:(android)