How to pass an object from one activity to another on Android

后端 未结 30 4059
遇见更好的自我
遇见更好的自我 2020-11-21 04:03

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity.

The code for t

30条回答
  •  死守一世寂寞
    2020-11-21 05:00

    In my experience there are three main solutions, each with their disadvantages and advantages:

    1. Implementing Parcelable

    2. Implementing Serializable

    3. Using a light-weight event bus library of some sort (for example, Greenrobot's EventBus or Square's Otto)

    Parcelable - fast and Android standard, but it has lots of boilerplate code and requires hard-coded strings for reference when pulling values out the intent (non-strongly typed).

    Serializable - close to zero boilerplate, but it is the slowest approach and also requires hard-coded strings when pulling values out the intent (non-strongly typed).

    Event Bus - zero boilerplate, fastest approach, and does not require hard-coded strings, but it does require an additional dependency (although usually lightweight, ~40 KB)

    I posted a very detailed comparison around these three approaches, including efficiency benchmarks.

提交回复
热议问题