5

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?

Arthur Dent
  • 1,248
  • 2
  • 12
  • 23
  • Remember `onDestroy` is not guaranteed to be called, see: http://stackoverflow.com/a/19608985/562139. I'd move any state-maintenance logic to `onStop` instead. – scorpiodawg Dec 22 '16 at 20:40

1 Answers1

0

Yes pretty much, altough you dont want the app to be killed, sometimes the System needs memory and trashes some Objects. The App itself is still there and will only call onResume() but doesnt have all the Objects that it created earlier.

Yalla T.
  • 3,707
  • 3
  • 23
  • 36
  • 1
    This would explain my problems. Do you know if there is any official documentation about this, or do you have any links to background information that explains this behaviour? If this is correct it would help to create the objects in onResume instead of onCreate. I will try this, but sadly I'll have to wait for the next release to see if it works, because even with massive testing the problem never appeared on my devices. – Arthur Dent Feb 19 '13 at 15:52