Update Tabhost Imageview From Another Activity
I currently have a tabhost with 5 tabs. Over one of the tabs I have an ImageView that when the tabs are created it pulls data via POST to display a number. I am wondering how from
Solution 1:
Based on the information given, two options that immediately come to mind are:
- Send a broadcast from the tab activity (e.g. Rate.java) and have the activity hosting the ImageView listen for it.
- Create some sort of BaseActivity (extending
Activity
) that takes a custom Listener interface with an update method. Have your tab activities extend that BaseActivity and the activity with your ImageView implement it. You can then call the update method on the listener from your tab activities (instantiate them as a BaseActivity and pass along the listener) and make the activity with the ImageView act upon it.
//Edit per request:
A good starting point for information about broadcasts and receivers is the documentation for the BroadcastReceiver
. In your case it's probably easiest to just create them in code.
A minimal example will contain something like the following:
BroadcastSendingActivity:
publicclassBroadcastSendingActivityextendsActivity {
publicstaticfinalStringUPDATE_IMAGEVIEW="UPDATE_IMAGEVIEW";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sender);
Intenti=newIntent();
i.setAction(UPDATE_IMAGEVIEW);
sendBroadcast(i);
}
}
BroadcastReceivingActivity:
publicclassBroadcastReceivingActivityextendsActivity {
privateBroadcastReceiver mReceiver;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver);
}
@OverrideprotectedvoidonPause() {
super.onPause();
unregisterReceiver();
}
@OverrideprotectedvoidonResume() {
super.onResume();
registerReceiver();
}
privatevoidregisterReceiver() {
if (mReceiver == null) {
mReceiver = newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
if (intent.getAction().equals(BroadcastSendingActivity.UPDATE_IMAGEVIEW)) {
// code to update imageview...
}
}
};
}
getApplicationContext().registerReceiver(mReceiver, newIntentFilter(BroadcastSendingActivity.UPDATE_IMAGEVIEW));
}
privatevoidunregisterReceiver() {
if (mReceiver != null) {
getApplicationContext().unregisterReceiver(mReceiver);
}
}
}
Note that I did not test the code, but I'm sure you'll be able to figure out any mistakes I might've made. :)
Post a Comment for "Update Tabhost Imageview From Another Activity"