How to exchange data (objects) between different Android Activities?

前端 未结 4 860
暗喜
暗喜 2021-02-09 17:35

What is proper way to exchange data or objects between different Android Activities?

Welcome screen <-> Main Screen <-> Startup routines <-> Processing data <

4条回答
  •  你的背包
    2021-02-09 18:27

    The answer is: It Depends.

    Depending on the architecture of your application, you may want to:

    • Store your data in a custom ContentProvider, and pass around URI references to it --- if your application is based around a database, this is the way to go, as it allows other applications to refer directly to your data items;

    • Have your Activities communicate by sending each other Intents, with the data packaged inside custom Intent data fields --- if you're only using very small items of data, such as names or URIs, this is a simple way of managing things, but it breaks down for larger items;

    • Have all your Activities running inside a single process, and store your data in shared Java objects --- generally not recommended, but suitable for specialised applications such as games (but bear in mind issues to do with application lifecycle!).

    My apps tend to use a combination of the first two: the data primarily lives in a ContentProvider, but I also use intents to send out-of-band information between the ContentProvider and the activities when such data won't easily fit in the ContentProvider API.

提交回复
热议问题