Android中的数据共享-application context

最近在做一个模拟Android京东商城的练手项目,其中一个需求是:当用户登录后,如何让用户的id,name,phone,address等信息实现整个应用的数据共享呢? 
在两个activity之间传递数据,自然联想到比较常用方法,即通过intent意图绑定一个bundle对象进行传递。然而在多个松耦合的Activity中如何更好的实现数据的传递呢?在各大IT论坛博客中终于学习到了一种更好的解决办法:Application Context。

1.创建一个android.app.Application的子类,生成Setter&Getter

package com.example.jds.util;

import android.app.Application;

public class UserApplication extends Application {

    private int id;
    private String name;
    private String pass;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

2.在AndroidManifest.xml中配置这个类

<application
        android:name="com.example.jds.util.UserApplication"
        android:icon="@drawable/appimage"
        android:label="@string/app_name"
        android:theme="@style/jdsTheme" >
        <activity
        ...
        </activity>
...
</application>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.如何使用?

// UserApplication 是Android建立的一个全局可用的实例
//在任意一个activity中使用该方法可以为变量赋值
int id = 1;
((UserApplication) getApplication()).setId(id);

//在任意一个activity中使用该方法可以获取变量的值
String name = ((UserApplication) getApplication()).getName();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

附参考英文原文: 
The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable (for instance, a singleton) is a common Java way of achieving this. I have found however, that a more elegant way in Android is to associate your state with the Application context. 
As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application. 
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 and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/LeoLeoHan/article/details/46486321
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值