可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
Either
- cache the Context object via constructor in your adapter, or
- get it from your view.
Or as a last resort,
- add - FLAG_ACTIVITY_NEW_TASK flag to your intent:
_
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack.
回答2:
I solved it with "addFlags" instead of "setFlags"
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
According to the documentation it does:
Add additional flags to the intent (or with existing flags value).
EDIT
Be aware if you are using flags that you change the history stack as Alex Volovoy's answer says:
...avoid setting flags as it will interfere with normal flow of event and history stack.
回答3:
Instead of using (getApplicationContext)
use YourActivity.this
回答4:
If you got error because of using create chooser like below:
Intent sharingIntent = new Intent(Intent.ACTION_VIEW); sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sharingIntent.setData(Uri.parse("http://google.com")); startActivity(Intent.createChooser(sharingIntent, "Open With"));
Set the flag to create chooser like this :
Intent sharingIntent = new Intent(Intent.ACTION_VIEW); sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sharingIntent.setData(Uri.parse("http://google.com")); Intent chooserIntent = Intent.createChooser(sharingIntent, "Open With"); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(chooserIntent);
回答5:
I think maybe you are implementing the OnClickListener in the wrong place - usually you should definitely implement an OnItemClickListener in your Activity and set it on the ListView instead, or you will get problems with your events...
回答6:
In addition: if you show links in listview in fragment, do not create it like this
adapter = new ListAdapter(getActivity().getApplicationContext(),mStrings);
instead call
adapter = new ListAdapter(getActivity(),mStrings);
adapter works fine in both cases, but links work only in last one.
回答7:
CustomAdapter mAdapter = new CustomAdapter( getApplicationContext(), yourlist);
or
Context mContext = getAppliactionContext(); CustomAdapter mAdapter = new CustomAdapter( mContext, yourlist);
change to below
CustomAdapter mAdapter = new CustomAdapter( this, yourlist);
回答8:
See, if you are creating an intent within a listiner in some method
override onClick (View v).
then call the context through this view as well:
v.getContext ()
There will not even need SetFlags ...
回答9:
For anybody getting this on Xamarin.Android (MonoDroid) even when StartActivity is called from activity - this is actually Xamarin bug with new ART runtime, see https://bugzilla.xamarin.com/show_bug.cgi?id=17630
回答10:
In my opinion, it's better to use the method of startActivity()
just in the your code of the Activity.class
. If you use that in the Adapter
or other class, it will result in that.
回答11:
This error goes when startactivity doesn't know which is his activity. So you must add activity before startActivity()
you must set
activity.startActivity(yourIntent);
回答12:
Elaborating Alex Volovoy's answer a little more -
in case u are getting this problem with fragments, getActivity() works fine to get the context
In Other Cases:
If you don't want to use-
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//not recommend
then make a function like this in your OutsideClass -
public void gettingContext(Context context){ real_context = context;//where real_context is a global variable of type Context }
Now,in your main activity when ever you make a new OutsideClass call the above method immediately after you define the OutsideClass giving the activity's context as argument. Also in your main activity make a function-
public void startNewActivity(final String activity_to_start) { if(activity_to_start.equals("ACTIVITY_KEY")); //ACTIVITY_KEY-is a custom key,just to //differentiate different activities Intent i = new Intent(MainActivity.this, ActivityToStartName.class); activity_context.startActivity(i); }//you can make a if-else ladder or use switch-case
now come back to your OutsideClass,and to start new activity do something like this-
@Override public void onClick(View v) { ........ case R.id.any_button: MainActivity mainAct = (MainActivity) real_context; mainAct.startNewActivity("ACTIVITY_KEY"); break; } ........ }
This way you will be able to start different activities called from different OutsideClass without messing up with flags.
Note-Try not to cache context object via constructor for fragment(with adapter,its fine).A fragment should have a empty constructor otherwise application crashes in some scenarios.
remember to call
OutsideClass.gettingContext(Context context);
in the onResume() function as well.
回答13:
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.
回答14:
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(viewIntent);
i hope this will work.
回答15:
I had the same problem. The problem is with context. If you want to open any links (for example share any link through chooser) pass activity context, not application context.
Dont forget to add myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if you are not in your activity.
回答16:
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.
回答17:
Intent i= new Intent(context, NextActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
回答18:
use this code. works fine for me; Share Something from Outside of an activity
Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); // Append Text String Text = "Your Text Here" intent.putExtra(Intent.EXTRA_TEXT, Text); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Intent shareIntent = Intent.createChooser(intent,"Share . . . "); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); G.context.getApplicationContext().startActivity(shareIntent);
回答19:
If you are invoking share Intent in Cordova plugin, setting the Flag will not help. Instead use this -
cordova.getActivity().startActivity(Intent.createChooser(shareIntent, "title"));
回答20:
My situation was a little different, I'm testing my app using Espresso
and I had to launch my Activity with ActivityTestRule
from the instrumentation Context
(which is not the one coming from an Activity
).
fun intent(context: Context) = Intent(context, HomeActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
I had to change the flags and add an or
bitwise ( |
in Java) with Intent.FLAG_ACTIVITY_NEW_TASK
So it results in:
fun intent(context: Context) = Intent(context, HomeActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)