现在要做一个项目,多个Activity之间要共享数据,所以要考虑共享数据的方式。

其实有如下5种方式:

1.基于消息的通信机制  Intent ---bundle ,extra

数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream, 或者LinkList链表等等数据类型就不太好用。

2. 利用static静态数据,public static成员变量;

3.基于外部存储的传输,  File/Preference/ Sqlite ,如果要针对第三方应用需要Content Provider

4.基于IPC的通信机制context 与Service之间的传输,如Activity与Service之间的通信,定义AIDL接口文件。

5. 基于Application Context

这里面我觉得第五种方法更具普适性,在网上找了篇讲解得好的文章,原文如下:

在Android中使用Intent在两个Activity间传递数据时,只能是基本类型数据,或者是序列化对象。Intent是一种基于消息的进程内和进程间通信模型,当我们需要在我们应用程序内部,多个Activity间进行复杂数据对象共享交互时,使用Intent就显得很不方便。此时,我们就需要一种数据共享的机制来实现。当然,直接使用java语言中的静态变量是可以的,但在Android中有更为优雅的实现方式。

The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.

--如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。

As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.

--每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。

The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):

--方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}

对于Application的生命周期,今天测试了一下,Application类型在该APP被install的时候就已经实例化了,并且onCreate也已经被调用了。

如果需要创建类型里面可能需要用到的对象的话,就可以在构造函数里面实现,但是如果需要将该类型bind Service或者registerReceiver等操作时,需要将这些操作放到onCreate中,否则会抛出异常。其原因主要是这个对象还没有创建完成,此时你用这个对象来bindservice必然会出现意想不到的情况,所以在使用时还需要注意。

多个Activity之间共享数据的方式的更多相关文章

  1. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

  2. 两个Activity之间共享数据、互相访问的另一种方式的实现

    本帖最后由 勇敢的心_ 于 2010-9-29 11:51 编辑 本人从windows编程转过来学习Android开发,一直在想如果两个Activity之间能够像C#或delphi中的Form一样,可 ...

  3. JAVA多线程提高四:多个线程之间共享数据的方式

    多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...

  4. JAVA多线程学习八-多个线程之间共享数据的方式

    多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...

  5. 面试之路(13)-android apk之间共享数据的方式以及shareUserId详解

    1.通过content Provider/sharedPreferrence 2.通过shareUserId 我们详细介绍一下shareUserId: Android App Sandbox(andr ...

  6. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

  7. Android应用程序组件Content Provider在应用程序之间共享数据的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6967204 在Android系统中,不同的应用 ...

  8. 使用 Bundle 在 Activity 之间交换数据

    [toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...

  9. 多线程(四) 实现线程范围内模块之间共享数据及线程间数据独立(Map集合)

    多个线程访问共享对象和数据的方式 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代码 ...

随机推荐

  1. This Debug perspective is designed to support application debugging.it incorporates views for displaying the debug stack,variables and breakpoint mamagement

    使用IDE(Eclipse Version:Neon.2 Release (4.6.2)),出现以下提示信息: This kind of launch is configured to openthe ...

  2. protocol buffer 编码

    protocol buffer能够跨平台提供轻量的序列化和反序列化,得益于其平台无关的编码格式,本文就介绍下其中的编码格式. Varints 在protocol buffer中大量使用到了Varint ...

  3. vue安装,router-link的一些属性,用法,tag active-class,to,replace,exex等等

    第一步:$ npm install -g vue-cli 第二部:$ vue init webpack my-projectName 下面内容转载自:https://www.cnblogs.com/c ...

  4. CentOS 7 使用OwnCloud建立私有云储存网盘

    使用OwnCloud建立属于自己私有的云存储网盘 OwnCloud概述: OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCl ...

  5. configparser配置文件处理

    创建一个configparser格式的文档: import configparser config = configparser.ConfigParser()config["DEFAULT& ...

  6. oracle 死锁

    oracle 死锁 --查用户名,查客户端机器 SELECT distinct s.username,s.MACHINE, s.sid||','||s.serial# FROM gv$session ...

  7. 必须添加对程序集"System.Core"的引用

    在项目下的web.config中添加 <compilation debug="true" targetFramework="4.0"> <as ...

  8. AI 基础

    what AI ? 人工智能(Artificial Intelligence),英文缩写为AI. 人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的 ...

  9. DataTables页面列点击排序

    <!-- DataTables --> <script type="text/javascript" charset="utf8" src=& ...

  10. Python基础(三)文件操作

    [对文件进行循环操作] fw = open('nhy','w') for line in fw: print('line:',line)   #直接循环文件对象,每次循环的时候就是取每一行的数据 fw ...