TabHost的各个Tab页,都是由activity组成。
现在,某个子Activity中处理数据后,要自动跳转到另外一个Tab页中。这样,需要有个TabHost页和子Activity之间的通讯的机制。
子Activity通知TabHost:
1、通过广播方式
1)在TabHost中定义广播
定义变量
protected MessageBroadcastReceiver myReceiver = null;
在onCreate中注册:
加入receiveBroadcast();
private void receiveBroadcast() {
myReceiver = new MessageBroadcastReceiver();
//Log.d(LOG_TAG, "receiveBroadcast() ::::: " + myReceiver.hashCode());
IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
registerReceiver(myReceiver, intentFilter);
}
在onDestroy中释放广播
if (myReceiver != null)
unregisterReceiver(myReceiver);
MessageBroadcastReceiver类定义:
public class MessageBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if (action.equals("android.intent.action.MAIN")) {
switch (intent.getIntExtra("msg", 0)) {
case Constant.BROADCAST_KEY_TABHOST_CHANGE: {
int tabIndex = intent.getIntExtra("tabIndex", 0);
animateChangeTab(tabIndex);
}
}
}
} catch (Exception e) {
e.printStackTrace();
Log.v(LOG_TAG, e.getMessage());
}
}
}
2)在子Activity中调用
Intent intent = new Intent("android.intent.action.MAIN");
intent.putExtra("msg", Constant.BROADCAST_KEY_TABHOST_CHANGE);
intent.putExtra("tabIndex", 0);
sendBroadcast(intent);
2、通过handler接收消息
1)在TabHost中定义Handler
// 得到主线程的Looper对象
Looper looper = Looper.myLooper();
// 这个myHandler由主线程创建所以它与主线程关联
public MessageHandler msgHandler = new MessageHandler(looper);
private void showMessage(String message) {
// 发送本次信息
msgHandler.removeMessages(0);
Message m = msgHandler.obtainMessage(1, 1, 1, message); // 构造要传递的消息
msgHandler.sendMessage(m); // 发送消息: 系统会自动调用handleMessage 方法来处理消息
}
public class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) { // 处理消息
switch (msg.what) {
case Constant.BROADCAST_KEY_TABHOST_CHANGE:
int tabIndex = (Integer) msg.obj;
animateChangeTab(tabIndex);
break;
default:
Toast.makeText(getApplicationContext(), msg.obj.toString(),
Toast.LENGTH_SHORT).show();
break;
}
}
}
2)在子Activity中调用
BasketballMainActivity t= (BasketballMainActivity) getParent();
Message message = new Message();
message.what = Constant.BROADCAST_KEY_TABHOST_CHANGE;
message.obj = 0;
t.msgHandler.sendMessage(message);
TabHost通知子Activity:
1、子Activity是活动的
在TabHost中增加
Activity currentActivity = getCurrentActivity();
if (currentActivity instanceof A) {
((A) currentActivity).处理方法XXX;
}
2、子Activity是不是活动的
在子Activity的
@Override
protected void onResume() {
//添加逻辑即可
}
现在,某个子Activity中处理数据后,要自动跳转到另外一个Tab页中。这样,需要有个TabHost页和子Activity之间的通讯的机制。
子Activity通知TabHost:
1、通过广播方式
1)在TabHost中定义广播
定义变量
protected MessageBroadcastReceiver myReceiver = null;
在onCreate中注册:
加入receiveBroadcast();
private void receiveBroadcast() {
myReceiver = new MessageBroadcastReceiver();
//Log.d(LOG_TAG, "receiveBroadcast() ::::: " + myReceiver.hashCode());
IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
registerReceiver(myReceiver, intentFilter);
}
在onDestroy中释放广播
if (myReceiver != null)
unregisterReceiver(myReceiver);
MessageBroadcastReceiver类定义:
public class MessageBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if (action.equals("android.intent.action.MAIN")) {
switch (intent.getIntExtra("msg", 0)) {
case Constant.BROADCAST_KEY_TABHOST_CHANGE: {
int tabIndex = intent.getIntExtra("tabIndex", 0);
animateChangeTab(tabIndex);
}
}
}
} catch (Exception e) {
e.printStackTrace();
Log.v(LOG_TAG, e.getMessage());
}
}
}
2)在子Activity中调用
Intent intent = new Intent("android.intent.action.MAIN");
intent.putExtra("msg", Constant.BROADCAST_KEY_TABHOST_CHANGE);
intent.putExtra("tabIndex", 0);
sendBroadcast(intent);
2、通过handler接收消息
1)在TabHost中定义Handler
// 得到主线程的Looper对象
Looper looper = Looper.myLooper();
// 这个myHandler由主线程创建所以它与主线程关联
public MessageHandler msgHandler = new MessageHandler(looper);
private void showMessage(String message) {
// 发送本次信息
msgHandler.removeMessages(0);
Message m = msgHandler.obtainMessage(1, 1, 1, message); // 构造要传递的消息
msgHandler.sendMessage(m); // 发送消息: 系统会自动调用handleMessage 方法来处理消息
}
public class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) { // 处理消息
switch (msg.what) {
case Constant.BROADCAST_KEY_TABHOST_CHANGE:
int tabIndex = (Integer) msg.obj;
animateChangeTab(tabIndex);
break;
default:
Toast.makeText(getApplicationContext(), msg.obj.toString(),
Toast.LENGTH_SHORT).show();
break;
}
}
}
2)在子Activity中调用
BasketballMainActivity t= (BasketballMainActivity) getParent();
Message message = new Message();
message.what = Constant.BROADCAST_KEY_TABHOST_CHANGE;
message.obj = 0;
t.msgHandler.sendMessage(message);
TabHost通知子Activity:
1、子Activity是活动的
在TabHost中增加
Activity currentActivity = getCurrentActivity();
if (currentActivity instanceof A) {
((A) currentActivity).处理方法XXX;
}
2、子Activity是不是活动的
在子Activity的
@Override
protected void onResume() {
//添加逻辑即可
}