简体   繁体   English

Android Retrofit如何使通话可拆分?

[英]Android Retrofit how to make a Call parcelable?

I use retrofit as ReST Client in my android project and have different calls depending on which activity started an intent. 我在我的android项目中使用ReST Client作为翻新客户端,并根据启动了意图的活动进行了不同的调用。 I want to pass a Retrofit Call<> as parcelable. 我想将包裹传递给Retrofit Call <>。 How can I achieve this? 我该如何实现?

Retrofit Call is not Parcelable and you should not pass it between activities (even if it was) since they have different Lifecycles. 翻新呼叫是不可分批的,并且您不应在活动之间传递(即使是在活动之间),因为它们具有不同的生命周期。

I suggest having another class (like CallManager) which both activities have access to. 我建议另一个活动都可以访问的另一个类(如CallManager)。

If you have Dependency Injection inside your app, you can inject it to both your activities. 如果您的应用程序中有依赖项注入,则可以将其注入到两个活动中。 If not, just instantiate the class in a custom Application class and have it inside a public variable inside the Application class. 如果没有,则只需在自定义Application类中实例化该类,并将其放在Application类内的公共变量中。 That way you can access it inside your activities like ((MyApplication) getApplicationContext()).getCallManager().callSomething(...) . 这样,您可以在((MyApplication) getApplicationContext()).getCallManager().callSomething(...)这样的活动中访问它。

I think you need to use a simple strategy while using retrofit 1.9 or 2 as it is not necessary at all to use parcelable inside retrofit call because retrofit calls having their own overridden methods and own cycle. 我认为您在使用翻新1.9或2时需要使用一种简单的策略,因为根本没有必要在可翻新内部调用中使用可分包,因为翻新调用具有自己的重写方法和自己的循环。 Parcelable is used for passing data between objects so it is a good practice to keep them aside. Parcelable用于在对象之间传递数据,因此将它们放在一边是一种好习惯。 You can make a response and request object like natural retrofit calls. 您可以做出响应并请求对象,例如自然的改造调用。 If you want you can use same objects which you used in your model class implementing parcelable in service ( where you define the end points ). 如果需要,可以使用在模型类中使用的可实现服务中可包裹的对象(定义终点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM