---
title: "Android--- 利用Application实现全局数据共享"
author: "chenchuangfeng"
date: "2013-01-25T23:01:32Z"
tags: []
categories: ["移动开发"]
slug: "android-application"
draft: false
lastmod: "2013-01-25T23:01:32Z"
id: 1775951
url: "https://chenchuangfeng.iteye.com/blog/1775951"
---

引官方api文档一段话

```
      Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
     
       大意：Application 是为那些需要控制全局应用状态准备的，你可以通过在AndroidManifest.xml中的 application标签来注册自己继承实现这个类的子类，从而能在你应用程序进程被创建时候实例化这个类。
```

 

     

 

     每个应用启动的时候，都会创建一个Application对象，所以可以继承Application来定义自己想要的Application子类，并在AndroidManifest.xml中修改appilcation 标签的android:name ="Application子类名"，使得启动应用程序进程的时候，会自动实例化Application的子类。

 

      Application对象在整个应用中是唯一，相当于单例模式，所以使用.getApplication()方法时候，返回的都是同一个对象，这样一来，我们就可以在Application子类中定义我们的各种变量和方法，达到在全局环境中共享同一个Application子类实例来传递数据,