出處:http://blog.csdn.net/veryitman/article/details/6611138java
感謝原文做者,整個邏輯很清楚,這備份下android
多個 Activity 之間能夠經過 Application 共享數據,在這裏我就讓兩個 Activity 共享 Handler(更新UI,我通常使用 Handler),主 Activity 中更新 UI,另外一個 Activity 發送更新UI的消息。這樣就達到在主Activity更新UI的目的。好吧,具體看代碼!web
1. 主 Activity 的 main.xmlapp
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="changed before: This is MasterActivity!" /> <Button android:layout_marginTop="15dip" android:id="@+id/btn_to" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="To OtherActivity"/> </LinearLayout>
2. 主 Activity 的Java 代碼 ide
package mark.zhang; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MasterActivity extends Activity { // 用於msg.what值 private static final int CHANGED = 0x0010; private Button btn_to = null; private TextView tv = null; private MyHandler handler = null; private MyAPP mAPP = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mAPP = (MyAPP) getApplication(); handler = new MyHandler(); tv = (TextView) findViewById(R.id.tv); btn_to = (Button) findViewById(R.id.btn_to); // 設置監聽器 btn_to.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 設置共享變量 mAPP.setHandler(handler); // 啓動另外一個Activity Intent intent = new Intent(MasterActivity.this, ToChangeViewActivity.class); startActivity(intent); } }); } /** * 本身實現 Handler 處理消息更新UI * * @author mark */ final class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == CHANGED) { // 更新UI tv.setText("changed after: I have be changed by Other Activity!"); tv.setBackgroundColor(Color.BLUE); btn_to.setText("I have been changed!"); btn_to.setBackgroundColor(Color.RED); } } } }
3. 自實現Application佈局
package mark.zhang; import mark.zhang.MasterActivity.MyHandler; import android.app.Application; /** * 本身實現Application,實現數據共享 * * @author mark * */ public class MyAPP extends Application { // 共享變量 private MyHandler handler = null; // set方法 public void setHandler(MyHandler handler) { this.handler = handler; } // get方法 public MyHandler getHandler() { return handler; } }
4. 改變主Activity UI 的Activity this
該 Activity 是 ToChangeViewActivity,Java、以及佈局文件 show.xml 代碼以下。spa
package mark.zhang; import mark.zhang.MasterActivity.MyHandler; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class ToChangeViewActivity extends Activity { private static final int CHANGED = 0x0010; private MyAPP mAPP = null; private MyHandler mHandler = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show); mAPP = (MyAPP) getApplication(); // 得到該共享變量實例 mHandler = mAPP.getHandler(); findViewById(R.id.btn_chang).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 發送消息 mHandler.sendEmptyMessage(CHANGED); ToChangeViewActivity.this.finish(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello,MasterActivity!" /> <Button android:id="@+id/btn_chang" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="change the MasterActivityView..." /> </LinearLayout>
5. 修改manifest.xml文件
這裏主要注意兩點: .net
<1> 聲明 Applicationcode
<2> 註冊 ToChangeViewActivity
代碼,以下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mark.zhang" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:name=".MyAPP" android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MasterActivity" 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=".ToChangeViewActivity"></activity> </application> </manifest>
6. 運行效果
點擊 " To OtherActivity",進入 ToChangeViewActivity
再點擊「 change the MasterActivityView...」
改變效果
7. 最後思考
這裏只是兩個Activity之間交互,多個 Activity 之間須要考慮設置 launchMode 即 Activity 的加載模式,更多關於這方面的知識能夠參考:
http://blog.csdn.net/androidbluetooth/article/details/6547670