Android使用Application的好处
如果一个应用程序有2个入口的,1个入口程序打开修改其中的内容,怎么实现另一个入口的数据也修改呢?
下面就用到Application来实现数据的共享,因为一个应用程序只有一个Application,Application还有一个更重要的功能就是数据的初始化
Application的oncreate的函数比activity的函数早执行
public void onCreate() {
super.onCreate();
System.out.println("App oncreate");
}
下面来看一下如何实现2个应用程序直接的数据共享:
首先MainActivity,把MainActivity的xml文件修改为main1.xml
然后新建第二个Activity,命名为Main2,
然后是布局,main1.xml和main2.xml的布局一样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView" /> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/btsave" />
</LinearLayout>
然后新建一个java文件,命名为App,继承自Application
public class App extends Application {
private String textData="default";
public String getTextData() {
return textData;
}
public void setTextData(String textData) {
this.textData = textData;
}
//application的oncreate的函数比activity的函数早执行
@Override
public void onCreate() {
super.onCreate();
System.out.println("App oncreate");
}
@Override
public void onTerminate() {
super.onTerminate();
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
//内存清理的时候
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
}
}
这里需要注意的一点就是我们需要把这个App类注册到AndroidManifest中,只用这样我们才能够实现数据的共享:AndroidManifest中的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yb.DataStorage"> <application
android:name="com.example.yb.DataStorage.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- //会在界面上呈现2个图标,都是程序的入口-->
<activity
android:name="com.example.yb.DataStorage.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="com.example.yb.DataStorage.Main2"
android:label="Main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ServiceActivity"></activity>
</application> </manifest>
这个应用程序中,会有2个入口。因为我们在2个activity中,我们都注册了相同的内容:
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
下面是MainActivity的,main2和MainActivity一样,只要改下 setContentView(R.layout.main1);
public class MainActivity extends AppCompatActivity {
private TextView tv;
private EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Main1 oncreate");
setContentView(R.layout.main1);
tv= (TextView) findViewById(R.id.textView);
ed= (EditText) findViewById(R.id.editText);
tv.setText("共享的数据是:"+getApp().getTextData());
findViewById(R.id.btsave).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((App)getApplicationContext()).setTextData(ed.getText().toString());
tv.setText("共享的数据是:"+ed.getText().toString());
}
});
}
public App getApp(){
return (App) getApplicationContext();
}
}
这样就实了2个入口的程序数据共享,~~

Android使用Application的好处的更多相关文章
- Android笔记——Application的作用
一.Application是什么 Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象, ...
- [转]Android中Application类的用法
原文链接:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application ...
- Android代码优化----Application节点的模板写法及UI工具类
一. MyApplication类的编写: 新建一个类MyApplication,继承自Application.代码如下: MyApplication.java: package com.smyhva ...
- 【转】Android中Application类用法
转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...
- Android使用Application总结
对于application的使用,一般是 在Android源码中对他的描述是; * Base class for those who need to maintain global applicati ...
- 在android.app.Application中定义全局变量
在Android应用中使用全局变量,除了public的静态变量,还有更优雅的方式是使用android.app.Application. 启动Application时,系统会创建一个PID,即进程ID, ...
- 【转载】Android使用Application总结
Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...
- Android中用Application类实现全局变量
最近在项目中,遇到了application这个类,开始不知道有什么用,经过学习后才知道它的用途也蛮大的,举个例子,如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在 ...
- Android中Application全局方法(变量)的调用
Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象,用来存储系统的一些信息.通常我们是 ...
随机推荐
- javascript export excel
<input type="button" onclick="tableToExcel('tablename', 'name')" value=" ...
- 手把手教你玩转nginx负载均衡(五)----配置后端服务器组
引言 在前面几篇中,我们成功的搭建起了一台nginx服务器,所以我们要重复前面的步骤,把服务器的数量增加到3台以上,我这里已经建好了另外两台,分别是centos7-22,centos7-23,对应的i ...
- Xcode最好用的日志打印方法
一般打印日志都是用的系统自带的NSLog来打印的,假如项目做完了,要上线了,这些打印的日志就会很浪费性能,网上有很多的解决办法,我也是感觉网上的还是有点不方便,所以就自己又修改了一下,分享给大家. 网 ...
- 【Java EE 学习 82 下】【MAVEN整合Eclipse】【MAVEN的一些高级概念】
一.MAVEN整合Eclipse MAVEN是非常优秀,但是总是要开命令行敲命令是比较不爽的,我们已经习惯了使用IDE,所以还有一种将MAVEN整合到Eclipse的方法. 详情查看:http://w ...
- 关闭电脑SSD的磁盘碎片整理
小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...
- Spring MVC 学习 -- 创建过程
Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...
- list for循环中删除元素
Iterator.remove() is safe, you can use it like this: List<String> list = new ArrayList<> ...
- SQL谜题(楼层谜题)
Multiple DwellingsBaker, Cooper, Fletcher, Miller and Smith live on different floors of an apartment ...
- DataGridView控件行标题显示序号
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridViewX1.R ...
- linux安装jdk(非rpm命令)
首先查看当前linux上安装的jdk版本: java -version 复制build 后面的jdk信息 卸载: rpm -e java-1.6.0_22-fcs 或者 yum -y remove j ...