Android 通过Application 传递数据
</pre><pre>
package com.example.ApplicationTest; import android.app.Application; /**
* Created by chang on 14-10-1.
*/
public class App extends Application {
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String name; @Override
public void onCreate() {
super.onCreate(); setName("张三");
}
}
package com.example.ApplicationTest; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MyActivity extends Activity { private Button btnLaunch = null;
private App myApp; /**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btnLaunch = (Button)this.findViewById(R.id.btnLaunch);
myApp = (App)this.getApplication();
myApp.setName("hello"); btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MyActivity.this,OtherActivity.class);
startActivity(intent);
}
}); }
}
package com.example.ApplicationTest; import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by chang on 14-10-1.
*/
public class OtherActivity extends Activity {
private App myApp;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.other);
textView = (TextView)findViewById(R.id.showMsg);
myApp = (App)getApplication(); textView.setText(myApp.getName());
}
}
<? xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ApplicationTest"
android:versionCode="1"
android:versionName="1.0"> <uses-sdk android:minSdkVersion="19"/>
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:name=".App">
<activity
android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity> <activity android:name=".OtherActivity" > </activity>
</application>
</manifest>
Android 通过Application 传递数据的更多相关文章
- 通过Application传递数据代码
使用Application传递数据步骤如下:创建新class,取名MyApp,继承android.app.Application父类,并在MyApp中定义需要保存的属性 在整个Android程 ...
- Android 使用全局变量传递数据
使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...
- Android--通过Application传递数据
在整个Android程序中,有时需要保存某些全局的数据(如:用户信息),方便在程序的任何地方调用.在Activity之间数据传递中有一种比较使用的方式,就是全局对象,使用过J2EE的都应该知道Java ...
- 通过Application传递数据
1:通过Application传递数据 假如有一个Activity A, 跳转到 Activity B ,并需要推荐一些数据,通常的作法是Intent.putExtra() 让Intent携带,或者有 ...
- Android剪切板传递数据传递序列化对象数据
一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...
- Android 使用意图传递数据
使用意图传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <But ...
- android使用全局变量传递数据
android中Application是用来保存全局变量的,在package创建的时候就存在了,到所有的activity都被destroy掉之后才会被释放掉.所以当我们需要全局变量的时候只要在appl ...
- Android与NativeC传递数据不正确问题
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 2.3.3 这两天一直在调试一个BUG,具体为通过 NativeC 来处理上层Android ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
随机推荐
- 在ubuntu上安装npm
sudo apt install npm 执行上面的安装命令,会报错: Reading package lists... Done Building dependency tree Reading s ...
- heartbeat负载均衡详解
heartbeat高可用软件http://www.linux-ha.org/wiki/Main_Page 1 heartbeat作用通过heartbeat将资源(IP)从一台已经故障的计算机快速转移到 ...
- [luoguP2606] [ZJOI2010]排列计数(DP)
传送门 如果能够根据题意看出这是一个堆的话,那么就有些思路了.. 首先堆顶必须是最小元素,然后左右儿子可以预处理出来都有多少个数, 把剩余的数任意分配给两个儿子,用排列组合即可 dp(now) = d ...
- Java中NIO、BIO、AIO相关概念及应用场景
1.同步阻塞IO(JAVA BIO):同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时,服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通 ...
- Spoj-ODDDIV Odd Numbers of Divisors
Given a positive odd integer K and two positive integers low and high, determine how many integers b ...
- git status检测不到文件变化
SourceTree(Git)无法检测新增文件的解决方法 有时候使用git管理软件SourceTree会遇到往项目里新增了文件,软件却没有任何反应的问题,这多发生在git合并出错而只能重新git的情况 ...
- (4)Swing布局
一.FlowLayout-流布局 新增第一个组件后默认在第一行的居中位置,之后 组件按照加入的先后顺序按照设置的对齐方式从左向右排列 二.borderLayout-边框布局 容器划分为东.西.南.北. ...
- 51 NOD 1325 两棵树的问题
Discription 对于 100% 的数据, N<=50. solution: 发现N比较小,所以我们可以花O(N^2)的代价枚举两颗树的联通块的LCA分别是哪个点,然后现在问题就变成了:选 ...
- 第1章 SpringBoot 简介
一.什么是Spring Boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- Struts2 与SpringMVC之比较
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...