Application可实现数据共享

例如:

一.新建一个空的工程,并新建一个App类,继承自Application

public class App extends Application {
private String textData="default"; public void setTextData(String textData) {
this.textData = textData;
} public String getTextData() {
return textData;
}
}

二.新建两个布局资源文件main1,main2,同时新建一个Main2类。main1.xml与main2.xml的内容相同,如下所示:

三.重写Main2.java的onCreate函数(MainActivity同)

  private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
textView.setText("The share data is "+getApp().getTextData());
findViewById(R.id.btnSaveData).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((App)getApplication()).setTextData(editText.getText().toString());//保存数据到Application
textView.setText("The share data is "+editText.getText().toString());
}
});
}
public App getApp(){
return (App) getApplicationContext();
}

四.修改AndroidManifest配置文件

 <application
android:name = ".App"//自定义的Aplication
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="Main1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2"
android:label = "Main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

将两个Activity都定义为launcher类型。

五.运行结果

Application的作用的更多相关文章

  1. 浅谈C# application.DoEvent作用

    Application.DoEvents()的作用:处理所有的当前在消息队列中的Windows消息. private void button1_Click(object sender, EventAr ...

  2. android 中Application的作用

    转自:lieren666 博客地址:http://blog.csdn.net/lieren666/article/details/7598288 What is Application Applica ...

  3. Android笔记——Application的作用

    一.Application是什么 Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象, ...

  4. Android Application类作用简单说明

    每个程序运行时创建一个Application类的对象且只创建一个 Application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期. Application来进行一些,如: ...

  5. 内置对象session和application的作用是什么?

    Jsp调用Javeabean命令UseBean中有Scope设置,一般有Application session page等设置,Page就是每页重新产生usebean中的javabean新对象,一般情 ...

  6. Android 开发笔记“Application 理解”

    Android 中Application类用法 1.             Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时 ...

  7. JSP 9大内置对象以及作用

    1.HttpServletRequest的 request对象作用:代表请求对象,用来接收客户端通过http协议连接传输到服务器端的数据. 2.HttpServletResponse的response ...

  8. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q63-Q65)

    Question 63You are designing a SharePoint 2010 implementation that will be used by a company with a ...

  9. 【概念笔记】JavaEE - web part2

    IT`huhui前言录 续JavaEE - web part1 链接http://www.cnblogs.com/ithuhui/p/5930745.html, 持续修改更新. Cookie 1. 定 ...

随机推荐

  1. 用仿ActionScript的语法来编写html5——第五篇,Graphics绘图

    用仿ActionScript的语法来编写html5——第五篇,Graphics绘图 canvas本身就是一个Graphics,可以直接进行绘图在actionscript里面,每个Sprite都有一个G ...

  2. rem根元素的定义规则

    <script> !function(a) { function b() { var b = g.getBoundingClientRect().width; var winpix=1; ...

  3. python学习之路-第七天-python面向对象编程简介

    面向对象编程 在python中,同样是类和对象作为重要的组成部分. 而且在python中基本数据类型如int都是有封装类的,都有自己的方法,应该是和java里面的Integer类似吧 类包括域和方法: ...

  4. Delphi 正则表达式之TPerlRegEx 类的属性与方法(6): EscapeRegExChars 函数

    Delphi 正则表达式之TPerlRegEx 类的属性与方法(6): EscapeRegExChars 函数 // EscapeRegExChars 函数可以自动为特殊字符加转义符号 \ var   ...

  5. <context:annotation-config/> 的理解

    转载:http://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Sprin ...

  6. HackerRank - journey-to-the-moon 【并查集】

    HackerRank - journey-to-the-moon [并查集] 题意 有很多不同的宇航员,有些宇航员来自同一个国家,有些宇航员来自不同的国家,然后美国航天局想要选出两名来自不同国家的宇航 ...

  7. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  8. 集合遍历(Set,List,Map)

    一.Set遍历 定义一个set Set<String> books = new HashSet<String>(); books.add("Java"); ...

  9. 浏览器 Event对象 及 属性 的兼容处理

    摘自: http://blog.csdn.net/jiachunfeng/article/details/6448186 event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以, ...

  10. 添加一个vue全局守卫,主要用于用户登录时候验证

    //注册一个全局守卫,作用是在路由跳转钱,对路由进行判断,防止未登录用户跳转到其他页面 router.beforeEach((to, from, next) => { let token = l ...