简体   繁体   English

观察Android中的应用程序状态更改

[英]Observing application state changes in Android

I've come to Android from web programming, and in the web, we've got a number of libraries for tracking application state and observing changes: 我从Web编程进入Android,在Web上,我们有许多用于跟踪应用程序状态和观察更改的库:

  • Backbone models and collections 骨干模型和集合
  • Knockout.js Knockout.js
  • Flux/Redux 助焊剂/终极版
  • RxJS RxJS

How do apps track and observe global state in Android? 应用如何在Android中跟踪和观察全局状态? Obviously RxJS has an analog in RxJava, and I've seen some Redux ports, but are those common? 显然,RxJS在RxJava中有一个类似物,我已经看到了一些Redux端口,但是这些端口很常见吗? If not, what is typical? 如果没有,典型的是什么?

Edit : "global state" is ambiguous, and I don't have a good definition, but an example might be a bunch of user profiles fetched from the server. 编辑 :“全局状态”是模棱两可的,并且我没有一个很好的定义,但是一个例子可能是一堆从服务器获取的用户配置文件。 Multiple activities might need that information, and it'd be inefficient if they independently fetched it, so it's best to have a single store for them in the application. 多个活动可能需要该信息,并且如果它们独立地获取信息,效率会很低,因此最好在应用程序中为它们提供一个存储。

There are a lot of ways to communicate between layers. 层之间有很多通信方式。

1) You can use event bus approach. 1)您可以使用事件总线方法。 Otto or EventBus library. Otto或EventBus库。 Also, you can implement custom event bus using RxJava. 另外,您可以使用RxJava实现自定义事件总线。

2) You can use local broadcast receiver. 2)您可以使用本地广播接收器。

3) You can use just simple callbacks or observer pattern depending on the number of receivers. 3)您可以仅使用简单的回调或观察者模式,具体取决于接收者的数量。

4) You can use RxJava which is quite popular now in Android community. 4)您可以使用RxJava,它在Android社区中现在非常流行。

In addition to what @Anton said, the following are both ways to both observe and query data from anywhere in the application: 除了@Anton所说的以外,以下是从应用程序中的任何位置观察和查询数据的两种方式:

1)SQLLite Database Tutorial 1)SQLLite数据库教程

2)And the much more robust ContentProvider Google Docs 2)以及更强大的ContentProvider Google文档

The SQLLite Database will provide a database for simple storing and retrieving of object data. SQLLite数据库将提供一个数据库,用于简单地存储和检索对象数据。

The ContentProvider allows you to leverage Android's built in services for storing and retrieving data. ContentProvider允许您利用Android的内置服务来存储和检索数据。 If you want your data to be accessible by other applications, a ContentProvider allows you to make it available. 如果您希望其他应用程序可以访问您的数据,则可以使用ContentProvider使其可用。 So in a sense, you data would then become "global" to any application that wants to incorporate it. 因此,从某种意义上讲,您的数据将成为要合并到任何应用程序的“全局”文件。

EDIT 编辑

In my experience, I wouldn't use them unless I absolutely needed to be able to query larger sets of data. 根据我的经验,除非绝对需要能够查询更大的数据集,否则我不会使用它们。 They are a bit cumbersome. 他们有点麻烦。

For example, a big disadvantage of the SQLLite Database is you have to write ALL of your CRUD operations. 例如,SQLLite数据库的一大缺点是您必须编写所有CRUD操作。 To help alleviate that, there are some ORM frameworks that are available that can make it simpler to use standard CRUD functions with any tables you make in the database. 为了缓解这种情况,有一些可用的ORM框架可以简化对数据库中创建的任何表使用标准CRUD函数的过程。 If you do decide to go that route, here are five that are apparently recommended 5 recommended Android ORMs 如果您决定走这条路,显然推荐了五个,推荐的五个Android ORM

Another route that may be viable depending on what you are looking to do, is to use an API service like Parse to house and query your data. 根据您要执行的操作,另一种可行的方法是使用诸如Parse之类的API服务来容纳和查询您的数据。 Parse is open source now, and so you'll need to set it up on your own server, but it gives you the power of all of their built in querying. Parse现在是开源的,因此您需要在自己的服务器上进行设置,但是它为您提供了所有内置查询的功能。 (Note: the database is MongoDB). (注意:数据库是MongoDB)。

In short, getting the extra power to query takes some effort in Android but it is possible, and there are some viable solutions out there to help alleviate the burden. 简而言之,在Android中获得额外的查询功能会花费一些精力,但有可能,并且有一些可行的解决方案可帮助减轻负担。

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

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