FractalBob
FractalBob

Reputation: 3534

Need to stop an app that was launched via Context.startActivity();

My app launches an app to display images selected by the user, but it needs to be able to terminate the displayer app on certain conditions. Here's what I have so far:

Uri attachmentUri = Uri.fromFile(new File("/sdcard/" + filename));

attachmentViewIntent = new Intent(Intent.ACTION_VIEW);
attachmentViewIntent.setDataAndType(attachmentUri, contentType);

attachmentViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

try {
    mContext.startActivity(attachmentViewIntent);
} catch (Exception e) {
}

The images get displayed just fine, but I don't see how to kill the display app. I tried

        mContext.stopService(attachmentViewIntent);

but that had no effect (I'm not sure I want to stop the service anyway, just the displayer). Any ideas?

Upvotes: 0

Views: 246

Answers (1)

a.bertucci
a.bertucci

Reputation: 12142

Once started the new activity, it will be up to the user to stop it (or less) and you have no control on how and when it will happen.

Calling Context.stopService(Intent) is used to stop a working Service, hence you have no effect because you started an Activity instead.

EDIT:
If you are not familiar with the concept of task in Android you should absolutely take a look at the docs:

Upvotes: 1

Related Questions