多个Activity中经常使用同一变量时,使用Bundle则每次调用Activity都要设置。在android中想在整个应用中不同Activity多次重复使用相同的变量时利用使用Application context可以方便的实现。
1、新建一个类,继承自Application
package activity.com;
import android.app.Application;
public class MyApplication extends Application{
private String myString="start";
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
}
2、在其它Activity中调用
package activity.com;
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.TextView;
public class FirstActivity extends Activity {
private TextView myTextView = null;
private Button myButton = null;
MyApplication myApplication;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("This is first TextView");
myButton = (Button) findViewById(R.id.myButton);
myButton.setText("First Button");
myButton.setOnClickListener(new myOnClickListener());
myApplication=(MyApplication) getApplication();
}
class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
String state = myApplication.getMyString();
Log.d("TEMP", "First获取数据:"+state);
myApplication.setMyString("First");
Log.d("TEMP", "First更改数据为数据:"+"First");
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);
}
}
}
package activity.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity{
private Button myButton2 = null;
private TextView myTextView2 = null;
MyApplication myApplication;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
System.out.println("2->1");
myTextView2 =(TextView)findViewById(R.id.myTextView2);
myTextView2.setText("This is second TextView");
System.out.println("2->2");
myButton2 = (Button)findViewById(R.id.myButton2) ;
myButton2.setText("Second Button");
myButton2.setOnClickListener(new myOnClickListener());
System.out.println("2->3");
myApplication=(MyApplication) getApplication();
}
class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
String state = myApplication.getMyString();
Log.d("TEMP", "Second获取数据:"+state);
myApplication.setMyString("Second");
Log.d("TEMP", "Second更改数据为数据:"+"Second");
}
}
}
3、AndroidManifest.xml中的设置:
在 application的name标签中添加MyApplication,如下:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApplication">
4、变量在一个Activity中改变后,在其它Activity中可以获取改变后的值
04-25 04:32:23.361: DEBUG/TEMP(1130): First获取数据:start
04-25 04:32:23.361: DEBUG/TEMP(1130): First更改数据为数据:First
04-25 04:32:30.891: DEBUG/TEMP(1130): Second获取数据:First
04-25 04:32:30.891: DEBUG/TEMP(1130): Second更改数据为数据:Second
04-25 04:32:36.743: DEBUG/TEMP(1130): First获取数据:Second
04-25 04:32:36.743: DEBUG/TEMP(1130): First更改数据为数据:First
04-25 04:32:39.883: DEBUG/TEMP(1130): Second获取数据:First
04-25 04:32:39.883: DEBUG/TEMP(1130): Second更改数据为数据:Second
操作如图: