Reputation: 65
OK, I'm stumped :-(
As an experiment, I've been trying to start an Activity and then sometime later shut it down using killBackgroundProcesses from the ActivityManager package. I'm not getting any errors. It just doesn't seem to do anything.
The code below is narrowed down about as much as I can get it. Just as a test, I start the Activity (successfully) and then when my onPause method gets called, I try and and stop what I had just started (in my real code, this happens much later).
Typing this up, it occurred to me that perhaps using killBackgroundProcesses on an Activity that is currently on-screen may not be meaningful. If so, is there any other way to stop this Activity? I do not have source to this other app, I started it up, and now I just want it to go away. And I also realize that this is not a normal Activity lifecycle behavior, but at least for now, this is a special case for me.
I'd appreciate any insight or solutions.
Thanks!
Manifest:
...
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
...
Code:
public class MainActivity extends Activity
{
static String PACKAGE = "com.abc.def";
...
Intent i = getPackageManager().getLaunchIntentForPackage( PACKAGE );
startActivity( i );
...
@Override
protected void onPause()
{
// just to see if we can, kill the activity we just started
ActivityManager manager = (ActivityManager)getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE );
manager.killBackgroundProcesses( PACKAGE );
}
}
Upvotes: 1
Views: 1774
Reputation: 68177
Calling killBackgroundProcesses is allowed for Android framework (internal system or partner rom developers like HTC, Samsung, etc) only and not the third-party applications. Instead, you should call finish();
on your activity's onPause or onStop method to destroy it automatically when its gone from screen.
Upvotes: 1