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?
I also had the same problem. Check all the context that you have passed. For 'links' it needs Activity Context not Application context.
This are the place where you should check :
1.) If you used LayoutInflater then check what context you have passed.
2.) If you are using any Adapter check what context you have passed.
In addition: if you show links in listview in fragment, do not create it like this
instead call
adapter works fine in both cases, but links work only in last one.
Either
Or as a last resort,
_
Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack.
See, if you are creating an intent within a listiner in some method
then call the context through this view as well:
There will not even need SetFlags ...
Faced the same issue then implemented
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and got solved the problem.
There may be an another reason which is related to list view adapter.
you can see This blog, described it very well.
You can achieve it with addFlags instead of
setFlags
According to the documentation it does:
EDIT
Be aware if you are using flags that you change the history stack as Alex Volovoy's answer says: