Unpredictable behavior caused by PackageManager.DONT_KILL_APP

PackageManager.DONT_KILL_APP API docs say:

Be careful when you install this, as changing the state of components can lead to unpredictability of application behavior.

Unfortunately, they do not go into the details of what they mean by unpredictable behavior.

In my application, I toggle the active activity state on. First, the service activates 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 activity (one-top) is started again or is destroyed, it again establishes the dialogs:

@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);
}

, onResume(), onCreate() . , NullPointerExceptions onResume(), , onCreate() .

:

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

: PackageManager.DONT_KILL_APP? - , ?

+5
1

, , , , . onResume(), , .

0

All Articles