简体   繁体   中英

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. I want to pass a Retrofit Call<> as parcelable. 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.

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. That way you can access it inside your activities like ((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. Parcelable is used for passing data between objects so it is a good practice to keep them aside. 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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