The API docs of PackageManager.DONT_KILL_APP say:
Be careful when you set this since changing component states can make the containing application's behavior unpredictable.
Unfortunately they don't go into detail what they mean by unpredictable behaviour.
In my application I'm switching the enabled state of an activity. First a service enables the activity and starts it:
getPackageManager().setComponentEnabledSetting(
new ComponentName(MyService.this.getApplicationContext(),
MyActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
final Intent launchIntent = new Intent(context, MyActivity.class);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(launchIntent);
If the (single-top) activity is started again or destroyed it sets itself to diabled again:
@Override
protected void onDestroy() {
log.d("ON DESTROY");
super.onDestroy();
getPackageManager().setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
@Override
protected void onNewIntent(Intent intent) {
if (someCondition) {
getPackageManager().setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
finish();
return;
}
super.onNewIntent(intent);
}
Normally everything works fine, but sometimes objects are null in onResume()
that are created in onCreate()
and not touched anywhere else. I were not able to reconstruct this problem in the debugger, but I get many bug reports with NullPointerExceptions in onResume()
that are impossible if onCreate()
was really called previously.
A trivial example for this is:
private String s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
s = new String("");
...
}
@Override
protected void onResume() {
super.onResume();
...
s.equals(""); // rarely causes NullPointerException
...
}
My question is: Can this be the the unpredictable behaviour of PackageManager.DONT_KILL_APP
? Or does anyone have another idea how this can happen?