Roboguice (1.1 and 1.2 Snapshot) injecting the wrong context in onActivityResult
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?
He开发者_高级运维re'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)
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:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
scope.enter(this);
try {
...
} finally {
scope.exit(this);
}
}
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.
精彩评论