Android多個Activity之間共享類(數據)——Application Context

多個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
操作如圖:

源碼下載:http://up.aiwalls.com/2012/0425/20120425020541495.rar

摘自 love254443233的專欄

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。