2

When my service need to interact with the user it starts an activity:

Intent i = new Intent(context, Ringer_intent.class);
i.putExtra("PHONE_NUMBER", phone_number);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
// TODO: This prevents onDestroy --> why???
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

context.startActivity(i);

In order to make sure that Ringer_intent will not run twice I added

i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

And I also added to my activity

@Override
public void onNewIntent (Intent intent) {
}

I want my Ringer_intent activity to go away when it finishes. I do not want the user to be able to bring it back to the fg.
That the way it work w/o FLAG_ACTIVITY_SINGLE_TOP
But with FLAG_ACTIVITY_SINGLE_TOP my activity remains in the bg ...
I added the following:

@Override
public void onStop() { Log.i(LOG_TAG, "Ringer_intent.onStop()"); // For Debugging
    super.onStop();
}

@Override
public void onDestroy() { Log.i(LOG_TAG, "Ringer_intent.onDestroy()"); // For Debugging
    super.onDestroy();
    Log.e(LOG_TAG, "Still not dead - kill it!");
    // Bad idea - it kills my service as well !!!
    // android.os.Process.killProcess(android.os.Process.myPid());
}

So I can see that Androids is calling both
But my activity is still there ...
Any idea?
Thanks

Note:
I tried to call finish() from onDestroy() - did not help

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Ori
  • 53
  • 3
  • 8
  • Your question is extremely difficult to understand :-( Please post your manifest and try to explain what you want to do and what isn't working in a clearer way. – David Wasser Sep 23 '17 at 05:47
  • Also, you cannot call `Intent.setFlags()` the way you are doing it. When you call `setFlags()` it overwrites the flags with the argument you pass to `setFlags()`. This means that after calling `setFlags()` 3 times, the only flag that will be set will be the one you specified in the last call to `setFlags()`. You can use `addFlags()` instead of `setFlags()`, or you can call `setFlags()` once and pass all of the flags as the argument, like this: `setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP)` – David Wasser Sep 23 '17 at 05:50
  • Thanks!! What a mistake :-) As David explained - My last call to Intent.setFlags() had canceled FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS and that is why my activity UI remains available to the user at the bg. Nothing todo with FLAG_ACTIVITY_SINGLE_TOP :-) Thanks again. – Ori Sep 23 '17 at 10:58
  • Glad that solved your problem. I posted it as an answer which you can accept by clicking on the green checkmark next to the answer. That will remove the question from the list of open questions and may help someone else. – David Wasser Sep 23 '17 at 13:55

1 Answers1

0

You cannot call Intent.setFlags() the way you are doing it. When you call setFlags() it overwrites the flags with the argument you pass to setFlags(). This means that after calling setFlags() three times, the only flag that will be set will be the one you specified in the last call to setFlags(). You can use addFlags() instead of setFlags(), or you can call setFlags() once and pass all of the flags as the argument, like this: setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP)

David Wasser
  • 93,459
  • 16
  • 209
  • 274