Discussion:
finishActivity() does not stop child activity
Rams
2011-03-14 17:41:49 UTC
Permalink
OK, so I have a Service which I want to be able to start (3rd party)
Activities and stop them as necessary. A simple example would be
detecting that headphones are plugged in and starting the Music app,
then stopping it when they are unplugged,

I've explored a number of ways to do this, but the only way seems to
be for the Service to start an Activity which then starts a sub-
activity. This much, I've accomplished and is pretty straight-forward.

My issue comes with shutting down the child Activity. I know it can be
done, since I have knocked up a Proof Of Concept project to check this
functionality. My POC has an Activity with a 'Go' button with the
following code:


Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
Intent intent = new Intent();
intent.setComponent(new
ComponentName("com.android.spare_parts",
"com.android.spare_parts.SpareParts"));
startActivityForResult(intent, 17);

try {
Thread.sleep(3500);
}
catch (InterruptedException e) {
e.printStackTrace();
}

finishActivity(17);
}
});

This works exaclty as expected.

My issue is that when I incorporate this principle into my app, the
call to finishActivity() does nothing.

Services can't start another Activity for result, so it starts an
intermediary Activity. The intermediary Activity then starts the 3rd
Party Activity. All this works fine.

The issue is that when the intermediary calls finishActivity(), the
3rd Party Activity continues running. I don't understand this, since
my POC project does not suffer from this problem.

There is a suspicious warning message in the Android console stating,
"Activity is launching as a new task, so cancelling activity result.",
but I suspect that this might be erroneous...

The obvious difference between the POC project and my app is that my
app is a Service which starts an intermediary Activity to control the
3rd party Activity. Is it possible that the intermediary Activity is a
'child' of the Service and is therefore unable to finish sub-
Activities?

+---------+ +-----------------------+ +--------------------+
| Service | -> | Intermediary Activity | -> | 3rd Party Activity |
+---------+ +-----------------------+ +--------------------+
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Mark Murphy
2011-03-15 11:55:40 UTC
Permalink
Post by Rams
OK, so I have a Service which I want to be able to start (3rd party)
Activities and stop them as necessary.
If this is not impossible, it is a security hole.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Warescription: Three Android Books, Plus Updates, One Low Price!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Ramsay Domloge
2011-03-15 13:25:29 UTC
Permalink
Right, I appreciate that Services should not be able to stop 3rd Party
Activities, *unrestricted*.

But what about 3rd Party Activities which they (or an intermediary Activity)
*started*?

Are you saying that my Service should not be allowed to start an Activity
which, in turn, starts (and then stops) a 3rd Party Activity?

If so, why? If an Activity can start (and then stop) a 3rd Party Activity
using finishActivity(code), then what difference does it make if the
Activity was started by a Service, rather than a User?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Mark Murphy
2011-03-15 14:00:49 UTC
Permalink
Post by Ramsay Domloge
Right, I appreciate that Services should not be able to stop 3rd Party
Activities, unrestricted.
But what about 3rd Party Activities which they (or an intermediary Activity)
started?
Are you saying that my Service should not be allowed to start an Activity
which, in turn, starts (and then stops) a 3rd Party Activity?
If so, why? If an Activity can start (and then stop) a 3rd Party Activity
using finishActivity(code), then what difference does it make if the
Activity was started by a Service, rather than a User?
OK, I'll buy your basic argument. If I had to guess, this is a task
problem, but it's only a guess. I poked around the source code and
couldn't really narrow down where finishActivity() would determine
what exactly it is finishing, or not finishing, as the case may be.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Warescription: Three Android Books, Plus Updates, One Low Price!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Nadeem Hasan
2011-03-15 15:34:06 UTC
Permalink
Is your proxy activity getting destroyed when it starts the 3rd party
activity? This could be related to the lifecycle of your proxy activity.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Ramsay Domloge
2011-03-15 18:05:56 UTC
Permalink
Cracked it.

I removed android:launchMode="singleInstance" from the Proxy Activity
declaration in the Manifest and suddenly the Proxy was able to close the 3rd
Party Activity.

Many thanks for your help guys - your suggestions pointed me in the right
direction.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
jgaribay
2011-03-26 15:38:10 UTC
Permalink
Hi,

I have a similar issue and I am using the same approach:

Service -> ProxyActivity -> Activity

But the Activity I want to start and finish is an android activity
started with ACTION_CALL from ProxyActivity and it has singleInstance
launch mode in the manifest but it seems I can not change it.
According to the documentation startActivityForResult() should not be
used to launch activities that wont be run in the same task so that
would explain why my finishActivity() is not working.

Is there a workaround or other way to finish activities are not in the
same task?

Thanks
Juan Garibay
Post by Ramsay Domloge
Cracked it.
I removed android:launchMode="singleInstance"  from the Proxy Activity
declaration in the Manifest and suddenly the Proxy was able to close the 3rd
Party Activity.
Many thanks for your help guys - your suggestions pointed me in the right
direction.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Loading...