语境希望FLAG_ACTIVITY_NEW_TASK但我已经设置标志 [英] Context wants FLAG_ACTIVITY_NEW_TASK but I've already set that flag
问题描述
我已经创建了一个共同的可重复使用的类,我的创造一些常见的界面元素工作的公司。
I've created a common re-usable class for the company I work for to create some common interface elements.
类,需要在一个单一的参数作为结构:一个应用程序上下文。
The class, takes in a single parameter as in the construct: an application context.
的方法之一, ContentClickableRowWithIcon
,您可以传递一个意图用作点击动作。
one of the methods, ContentClickableRowWithIcon
allows you to pass in an intent to be used as the click action.
继承人的全部方法声明:
heres the full method declaration:
公开的LinearLayout ContentClickableRowWithIcon(可绘制图标,弦乐标题,意图我,最后布尔选配)
这最后一个属性也被用在onClickEvent,以确定是否要调用一个选择器或者只是去右转入意图。
that last attribute there is used in the onClickEvent to determine whether to invoke a Chooser or just go right into the intent.
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser) {
LinearLayout ll = new LinearLayout(mContext);
// .. LinerLayout construction, has nothing to do with the action
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is apparently getting ignored... (ps: i've tried i.setFlags as well)
final Intent intent = i;
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(chooser)
mContext.startActivity(Intent.createChooser(intent, "Complete With...")); // crashes here with: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
else
mContext.startActivity(intent); // this works fine
}
});
return ll;
}
正如评论,任何时间我不提供使用一个选择器的能力,一切工作正常(一切都在此列表中获得一个新的活动标志,即时通讯深知这一点,就当这个问题想通了清理)
As mentioned in the comments, anytime I dont provide the ability to use a chooser, everything works fine (everything in this list gets a new activity flag, im well aware of this and will cleanup when this issue is figured out)
我做的那一刻,抛出该异常:
android.util.AndroidRuntimeException:从活动背景以外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?
The moment I do, throws the exception:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我已经江郎才尽了......
I've run out of ideas...
///:值得注意的,在调试,标志属性的目的是设置为 268435456
与addFlags和 268435456
与setFlags,当达到使用的目的在onclick动作时间
/// : Worth noting, on debug, the flags attribute in the Intent is set to 268435456
with addFlags and 268435456
with setFlags, when it reaches the time to use the intent in the onClick action
推荐答案
问题已得到解决,我觉得这简直就是一个经营秩序的情况下
Problem has been fixed, I think this is simply the case of an "order of operation" scenario
继承人是什么让这件事的工作:
heres what allowed this thing to work:
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(chooser) {
Intent intent = Intent.createChooser(i, "Complete With");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
} else
mContext.startActivity(i);
}
});
也可以在方法声明中加入了最终修改器的参数
also added a "final" modifier to the parameter in the method declaration
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, final Intent i, final Boolean chooser)
这篇关于语境希望FLAG_ACTIVITY_NEW_TASK但我已经设置标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!