Reputation: 1067
I have some classes implementing some kind of visual fragments. (I call them ApetWidgets.) They have their own class hierarchy.
public class SignatureApetWidget extends AbstractApetWidget {
private void startCapture(ApetActivity act) {
Intent intent = new Intent(act, SignatureCaptureActivity.class);
intent.putExtra(...);
act.startActivityForResult(intent, CAPTURE_SIGNATURE_REQUEST);
}
One of them should start another Activity and wait for the result. However, although I have the reference to activity containing the widget, and though I could call the parentActivity's startActivityForResult, I couldn't intercept the result, because that goes to the parent activity as well.
I could catch the result by it, and delegate back the processing to my widget, but it seems bad practice, for I would have to move widget-specific logic from widget to the activity, and what is more: I would have to implement this delegation in all activity classes where I am planning to use the widgets).
I KNOW how (at least I know a way) to start the other activity. (It works fine.) However, I get no result from it. Extending and inheriting from the Activity or any other class is NOT an option (my widgets has their own class hierarchy).
So my question is: how could I start an activity for result from within an non-activity class without infering the encapsulation clause badly?
Thanks, Balage
Upvotes: 3
Views: 863
Reputation: 23952
I used such construction. First - implement callback method in your widget. Second - put link to your widget in intent. Third - call widget callback method from your Activity.
By the way - it sound like you have problems with your code design. If you want to get some user-depended result from secod Activity, it's better way to deal with result in your Activity class, not widget.
Upvotes: 0
Reputation: 6182
I think the best way to do this would be to use an interface and listener. If you use Fragments http://developer.android.com/guide/topics/fundamentals/fragments.html as your parent class you won't need to do this, this is the reason why google created them.
Basically when your activity fires onActivityResult(int requestCode, int resultCode, Intent data) you should call the correct function in ApetWidgets. You don't have to put any specific code in the Activity other then passing the info to your widget. Once again, if you use Fragment all of the activity lifecycle methods are already delegated and ready to override.
Upvotes: 2