Roboguice(1.1 和 1.2 快照)在 onActivityResult 中注入错误的上下文
onActivityResult() 是一个标准 Android 函数,在子 Activity 关闭后调用。不过,似乎并没有完全关闭。
我的子活动完成后,在父活动中调用 onActivityResult() 。此时,我的操作是在父级正在创建的新类中注入上下文(通过提供者,非辅助),使用子级刚刚返回给我的可分割信息作为该新类中的 @Assisted 参数班级。
然而,尽管在子进程上调用 finish() ,注入的上下文并不是父进程——而是子进程!这会杀死该程序。
我该如何解决这个问题?
这里有一些代码可以让您了解我在做什么。
在父级中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_NEW_EXERCISE)
{
if (resultCode == RESULT_OK)
{
EntityExercise exercise = (EntityExercise)data.getExtras().get("exercise");
addNewRoutineExerciseDetail(exercise);
//Toast.makeText(this, exercise.getName(), Toast.LENGTH_LONG).show();
}
}
}
public RoutineExerciseDetail addNewRoutineExerciseDetail(EntityExercise exercise)
{
RoutineExerciseDetail detail = detailFactory.create(exercise);
detail.setOnClickRelativeLayoutListener(mEditParamsOnClickListener);
return detail;
}
在子级中:
View.OnClickListener mListenerReturnExercise = new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent resultIntent = new Intent();
resultIntent.putExtra("exercise", (EntityExercise)v.getTag()); //Assuming it's the tag
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
};
RoutineExerciseDetail 的构造函数的参数:
@Inject
public RoutineExerciseDetail(ActivityBaseRoboOrm<DatabaseHelper> context, List<RoutineExerciseDetail> list,
@AddEditExercise TableLayout layout, @Assisted EntityExercise exercise)
onActivityResult() is a standard Android function that is called after a child Activity closes. However, it doesn't seem to close all the way.
After my child activity finishes, onActivityResult() is called in the parent. At this point, my action is to inject a context (through a provider, non-assisted) in a new class the parent is creating, using the parcelable information that the child has just given back to me for an @Assisted parameter in that new class.
However, despite finish() being called on the child, the context that is injected is not the parent--it is the child! This kills the program.
How do I get around this?
Here's some code that gives you an idea of what I'm doing.
In the parent:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_NEW_EXERCISE)
{
if (resultCode == RESULT_OK)
{
EntityExercise exercise = (EntityExercise)data.getExtras().get("exercise");
addNewRoutineExerciseDetail(exercise);
//Toast.makeText(this, exercise.getName(), Toast.LENGTH_LONG).show();
}
}
}
public RoutineExerciseDetail addNewRoutineExerciseDetail(EntityExercise exercise)
{
RoutineExerciseDetail detail = detailFactory.create(exercise);
detail.setOnClickRelativeLayoutListener(mEditParamsOnClickListener);
return detail;
}
In the child:
View.OnClickListener mListenerReturnExercise = new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent resultIntent = new Intent();
resultIntent.putExtra("exercise", (EntityExercise)v.getTag()); //Assuming it's the tag
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
};
RoutineExerciseDetail's constructor's parameters:
@Inject
public RoutineExerciseDetail(ActivityBaseRoboOrm<DatabaseHelper> context, List<RoutineExerciseDetail> list,
@AddEditExercise TableLayout layout, @Assisted EntityExercise exercise)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这在 RoboGuice 1.1 上会失败。
Activity.onActivityResult()
是一个有点不寻常的方法,因为它在调用活动的 onResume() 之前执行,因此 RoboGuice 不知道将上下文切换回调用者活动。RoboGuice 1.2 中的主要更改之一是修复此行为。如果您切换到 1.2 并按照这些说明将任何提供程序替换为 ContextScopedProviders,您应该走吧。
如果您需要继续使用 RoboGuice 1.1,您应该能够通过以下方式手动调整上下文范围:
Yes, this will fail on RoboGuice 1.1.
Activity.onActivityResult()
is a somewhat unusual method in that it executes before the activity's onResume() is called, thus RoboGuice doesn't know to switch the context back to the caller activity.One of the main changes in RoboGuice 1.2 is to fix this behavior. If you switch to 1.2 and replace any providers with ContextScopedProviders per these instructions, you should be good to go.
If you need to stay with RoboGuice 1.1, you should be able to scope your context manually the following way:
在 Android 的 ActivityForResult 方法中,您的请求代码在 Activity.then 中应该相同,只有这样您的代码才能工作。我希望它能帮助你。
In ActivityForResult method in Android your requestcode should be same in both the Activity.then and only then your code will work. I hope it will help you.