I have implemented a ListView
in my Android application. I bind to this ListView
using a custom subclass of the ArrayAdapter
class. Inside the overridden ArrayAdapter.getView(...)
method, I assign an OnClickListener
. In the onClick
method of the OnClickListener
, I want to launch a new activity. I get the exception:
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
How can I get the Context
that the ListView
(the current Activity
) is working under?
If you got error because of using create chooser like below:
Set the flag to create chooser like this :
My situation was a little different, I'm testing my app using
Espresso
and I had to launch my Activity withActivityTestRule
from the instrumentationContext
(which is not the one coming from anActivity
).I had to change the flags and add an
or
bitwise (|
in Java) withIntent.FLAG_ACTIVITY_NEW_TASK
So it results in:
i hope this will work.
Instead of using
(getApplicationContext)
useYourActivity.this
This error goes when startactivity doesn't know which is his activity. So you must add activity before startActivity()
you must set