1. 程式人生 > >android開發之在activity中控制另一個activity的UI更新

android開發之在activity中控制另一個activity的UI更新

第一種方法:

遇到一個問題,需要在一個activity中控制另一個acitivity做一些更新,沒想到傳遞handler的方法,通過以下方式解決。

1.在MyAPP中定義屬性handler

package jason.com; 
   
import jason.com.MasterActivity.MyHandler; 
import android.app.Application; 
   
/**
* 自己實現Application,實現資料共享
* @author jason
*/ 
public class MyAPP extends Application { 
// 共享變數 
private MyHandler handler = null; 
   
// set方法 
public void setHandler(MyHandler handler) { 
this.handler = handler; 
} 
   
// get方法 
public MyHandler getHandler() { 
return handler; 
} 
   
} 

 2、在主activity 中給MyAPP的屬性handler賦值

@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); 
} 
}); 
   
} 

 3、在另一個activity中獲取MyAPP中handler進行傳值

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(); 
} 
}); 
   
} 

第二種方法  

兩個Activity之間可以通過傳送廣播來控制另外一個activity的UI更新,比如團購APP裡:我下了個訂單之後,那麼我可以通過傳送廣播通知“我的訂單”裡面的內容內容自動更新。

其實很簡單,就是Broadcast的應用,替換了原來想要通過共享handler解決的思路

程式碼如下:

MainActivity:

package jason.broadcastinsteadofhanlderdemo; 
   
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
   
public class MainActivity extends Activity { 
   
    TextView textView; 
    Button sButton; 
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        textView = (TextView) findViewById(R.id.show); 
        sButton = (Button) findViewById(R.id.startAnother); 
        sButton.setOnClickListener(new OnClickListener() { 
               
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                startActivity(new Intent(MainActivity.this,AnotherActivity.class)); 
            } 
        }); 
        IntentFilter filter = new IntentFilter(AnotherActivity.action); 
        registerReceiver(broadcastReceiver, filter); 
    } 
   
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
   
        @Override 
        public void onReceive(Context context, Intent intent) { 
            // TODO Auto-generated method stub 
            textView.setText(intent.getExtras().getString("data")); 
        } 
    }; 
   
    protected void onDestroy() { 
        unregisterReceiver(broadcastReceiver); 
    }; 
} 

 AnotherActivity:

package jason.broadcastinsteadofhanlderdemo; 
   
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
   
public class AnotherActivity extends Activity { 
   
    public static final String action = "jason.broadcast.action"; 
   
    Button update; 
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.another); 
        update = (Button) findViewById(R.id.updateMain); 
        update.setOnClickListener(new OnClickListener() { 
   
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                Intent intent = new Intent(action); 
                intent.putExtra("data", "yes i am data"); 
                sendBroadcast(intent); 
                finish(); 
            } 
        }); 
    } 
}