在Android中Application只是用来保存应用程序上下文,核心还是在Activity。但我们可以简单的在Application中定义公有的静态变量,从而在多个Activity内调用,实现数据共享。代码如下:
public class myApp extends Application { public static HashMap<String, Object> hashMap = new HashMap<String, Object>(); public static ArrayList<String> strList = new ArrayList<String>(); public static ArrayList<Integer> intList = new ArrayList<Integer>(); }
利用HashMap<String, Object>可以实现任意数据类型的存储,也可以用ArrayList来存储简单类型。使用时只需要简单的使用myApp.hashMap和myApp.strList等就行了。