Application就一挺熟悉又陌生的类,今天看了一下API,大概了解了一下。写了一个demo,贴上来,备查。
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in yourAndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
API如就说,大概意思就Application可以保存全局变更,而且你还可以自定义,通过配置AndroidManifest.xml中的Application标签的name字段可以配置自定义的Application,并且你的应用一启动时,这个Application就会启动。
看了看方法,有四个, onConfigurationChanged(Configuration newConfig), onCreate(), onLowMemory(), onTerminate()。大概看了下下,做了下试验,只懂了一个onCreate(),它比入口Activity的onCreate()先初始化,应该也就是 which will cause that class to be instantiated for you when the process for your application/package is created. 不多说了,贴代码吧。
主Activty,在onCreate方法设置MyApplication的一个参数:
第二个Activity,用于调用MyApplication的值
最重要的一点,在AndroidManifest.xml要设置application
API如就说,大概意思就Application可以保存全局变更,而且你还可以自定义,通过配置AndroidManifest.xml中的Application标签的name字段可以配置自定义的Application,并且你的应用一启动时,这个Application就会启动。
看了看方法,有四个, onConfigurationChanged(Configuration newConfig), onCreate(), onLowMemory(), onTerminate()。大概看了下下,做了下试验,只懂了一个onCreate(),它比入口Activity的onCreate()先初始化,应该也就是 which will cause that class to be instantiated for you when the process for your application/package is created. 不多说了,贴代码吧。
自定义的Application,定义了两个变量,字符串变量params及一个Actitvity:
package com.ghli.applicationtest;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
//自定义的变量
private String param1;
private ApplicationTest firstAct;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public ApplicationTest getFirstAct() {
return firstAct;
}
public void setFirstAct(ApplicationTest firstAct) {
this.firstAct = firstAct;
}
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
}
}
主Activty,在onCreate方法设置MyApplication的一个参数:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ApplicationTest extends Activity {
private final static String TAG="ApplicationTest";
private EditText etContent;
private Button btnNext;
private TextView tvTitle;
private MyApplication myApplication;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myApplication = (MyApplication)getApplication();
myApplication.setFirstAct(this);
etContent = (EditText)findViewById(R.id.content);
btnNext = (Button)findViewById(R.id.next);
tvTitle = (TextView)findViewById(R.id.title);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置MyApplication中的值
myApplication.setParam1(etContent.getText().toString());
Intent intent = new Intent(ApplicationTest.this, Activity1.class);
startActivity(intent);
}
});
Log.v(TAG, "onCreate");
}
public void setTitle(String title){
tvTitle.setText(title);
}
}
第二个Activity,用于调用MyApplication的值
package com.ghli.applicationtest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity1 extends Activity {
private TextView tvContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
tvContent = (TextView)findViewById(R.id.content);
MyApplication myApplication = (MyApplication)getApplication();
tvContent.setText(myApplication.getParam1());
myApplication.getFirstAct().setTitle("在其它Activity中设置");
}
}
最重要的一点,在AndroidManifest.xml要设置application
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ghli.applicationtest"
android:versionCode="1"
android:versionName="1.0">
<application
android:name=".MyApplication" //这个相当重要
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ApplicationTest"
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=".Activity1"/>
</application>
</manifest>