2

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 ?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Florian Mac Langlade
  • 1,863
  • 7
  • 28
  • 57
  • keep in mind that ActivityX can no longer exists when ActivityY is displayed ... – Selvin Apr 16 '13 at 14:10
  • it does it exist, just that it's not visible to the user – Carnal Apr 16 '13 at 14:14
  • no ... it can be killed ... even if you can back to it(back stack), it doesn't mean that it will be the same instance ...it can be reacreated ... – Selvin Apr 16 '13 at 14:15
  • 1
    activityX is exists if developer does not call finish() while activityY is starting. its window focues state changes. – anzaidemirzoi Apr 16 '13 at 14:16
  • yes, it can be killed if he goes and uses up the memory from other apps. But still! – Carnal Apr 16 '13 at 14:18
  • But still! ... is better to use Service for such things ... and do not asume that more than one Activity can be running/active at the same time – Selvin Apr 16 '13 at 14:22
  • I don't understand. What is the difference between destroying an Activity from a Service or from another Activity? – Carnal Apr 16 '13 at 14:30
  • @Cranal because Activity in Paused/Stopped **cannot execute any code.** http://developer.android.com/training/basics/activity-lifecycle/starting.html ... when ActivityY becomes visible ActivityX gets Paused/Stopped state ... thats why for example we should dissable broadcast recivers in onPause and enable it again on onResume of Activity – Selvin Apr 17 '13 at 06:57
  • Activities on the back stack will not be reclaimed on low memory conditions unless your application's entire process is killed (and in this case your whole app dies, not only the non-visible activities). Memory management on Android happens at a process level, so Android will kill *processes* in the background not activities. Yes the documentation seems to indicate that it is the case but in fact it is not. – AngraX Mar 13 '14 at 17:31

3 Answers3

3

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

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • partally correct ... sendBroadcast(Intent i) should not be called from ActivityX but from Service(started from ActivityX before ActivityY is started) – Selvin Apr 16 '13 at 14:25
  • @Selvin. what if ther eis no service? I can directly call from activity ( I used localbroadcastmanager) – stinepike Apr 16 '13 at 14:26
  • @Selvin: why from a Service? And why do you think he started the Activity Y from a Service? He said, he started Activity Y from Activity X. – Carnal Apr 16 '13 at 14:28
  • @Carnal activity state: Paused In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and **cannot execute any code.** from http://developer.android.com/training/basics/activity-lifecycle/starting.html – Selvin Apr 17 '13 at 06:55
  • 1
    @Selvin: I need to test this, I'm sure I've been able to execute code with receiver on paused activities. – Carnal Apr 17 '13 at 08:48
  • @Selving.. you better check. because I have done this in so many tasks – stinepike Apr 17 '13 at 08:53
  • @StinePike so you had made it wrong ... even if you are able to do it, you shouldn't – Selvin Apr 17 '13 at 09:00
  • 1
    @Selvin: You're wrong mate, I did a test updating even the GUI when the Activity was onPause, and it works fine ;) – Carnal Apr 18 '13 at 09:46
  • you so wrong ... for example: we can avoid NetworkOnMainThreadException by dissabling StrictMode ... but should we ? No! ... http://stackoverflow.com/questions/3259907/android-can-background-activities-execute-code – Selvin Apr 18 '13 at 09:49
  • 1
    Lets give a post.. you guys post a question if you are free. or I will post when I am free – stinepike Apr 18 '13 at 09:59
  • 1
    So how do you explain this answer and all the up votes and the comments below the accepted answer? Here mate: http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l – Carnal Apr 18 '13 at 10:19
0

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.)

NPike
  • 13,136
  • 12
  • 63
  • 80
0
  • When you receive 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

dharmendra
  • 7,835
  • 5
  • 38
  • 71