Android的 - 交互与其他活动 [英] Android - Interact with an other activity
问题描述
所以,我有一个问题!
我有当一个按钮,用户点击时,会显示活动Ÿ名为X的活动。
我想,经过已收到活动点¯x发送的情况下,本活动可以被关闭。
I have got an activity called X. When the user click on a button, the activity Y is displayed. I want that this activity can be closed after have received an event sent by activity X.
你知道我该怎么做呢?
推荐答案
从十发 BroadcastMessage
的In y注册广播接收器
与相同的IntentFilter
。因此,从X您可以发送将由Ÿ活动的的onReceive
方法捉了predefined退出消息。在那里,你可以结束活动年。
Send a BroadcastMessage
from X. In y register a BroadcastReceiver
with same IntentFilter
. So from x you can send a predefined exit message which will be catched by y activity's onReceive
method. There you can end the activity Y.
例如:
Y中的活动
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey("exit")) {
finish();
}
}
}
不要忘记注册接收
dont forget to register the receiver
registerReceiver(
mMessageReceiver,
new IntentFilter(Constants.YOUR_INTENT_FILTER));
和注销
unregisterReceiver( mMessageReceiver);
以x活动:
send the broadcastmessage using `sendBroadcast(Intent i)`
对于这种内部消息的我preFER <一个href=\"https://www.google.com.bd/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&sqi=2&ved=0CCwQFjAA&url=http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html&ei=sFxtUdyILIfKrAeCz4CYBg&usg=AFQjCNFu8jEI2XY2MXPdmzmaudH4M_XsBg&bvm=bv.45175338,d.bmk\"相对=nofollow> LocalBoradcastManager
这篇关于Android的 - 交互与其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!