So, I've got a question !
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.
Do you know how can I do this ?
So, I've got a question !
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.
Do you know how can I do this ?
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.
For Example:
in Y activity
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));
and unregister
unregisterReceiver( mMessageReceiver);
IN X activity:
send the broadcastmessage using `sendBroadcast(Intent i)`
For this kind of internal messaging I prefer LocalBoradcastManager
Unfortunately you can not have two activities "running" at the same time. Activity X goes through its end of lifecycle when you start Activity Y.
See: http://developer.android.com/guide/components/activities.html
Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes. (The back stack is discussed more in the Tasks and Back Stack document.)
BroadcastMessage
from ActivityX
then you can set value of one Global boolean
you should check that value of bolean at the time of use of Activity Y
(it may in onCreate
)
in Activity Y
if you get this boolean value true
(or whatever you set at the time of call BroadcastMessage
) then simply call finish()
The thing is android not running two activity at the same time , so without having activity you can not finish it remotely