Question

Is it possible to start an activity from a non-Activity class with FLAG_ACTIVITY_CLEAR_TOP?

I have a non-Activity class that, upon certain conditions, needs to launch an intent to an activity. I'm currently doing it as such:

Intent newIntent = new Intent(context, myActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);

This works, but I do not wish to start the activity as a new task. Instead, I'd like to use FLAG_ACTIVITY_CLEAR_TOP, but this gives me the following runtime System.err:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Was it helpful?

Solution

You can combine the flags (ie: use Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP).

However, if the existing task does NOT have an active (non-finished) instance of myActivity in it, it will not clear anything and it will just create a new instance of myActivity on the top of the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top