Processing onActivityResult from outside of activity

I'm trying to create a helper class to start an Activity and get a return result ( startActivityForResult ) so that the developers do not write their own code onActivityResult and encapsulate complex internal details.

i.e.: caller code:

 MyIntent i = new MyIntent(); i.getMyData(new OnData() { public void onData(Bundle data) {....} ); 

I tried to create a "dummy" Activity inside MyIntent just to override onActivityResult , but the activity should be declared in the manifest, which tries to avoid the helper class. There is no "setOnActivityResult" that would be a good alternative.

So, how to create the Activity code and β€œattach” it so that it has the correct state?

After creating a new Activity() I would like to call the "attach" operation, which is internal.

+7
source share
2 answers

So, how to create activity programmatically and β€œattach” it so that it has the correct state?

This is not possible, sorry.

I'm trying to create a helper class to start an Activity and get the result return (startActivityForResult) so that the developers do not write their own code onActivityResult and encapsulate complex internal details.

This is not possible, sorry. The closest you can get is how the JAR works with the integrated barcode scanner - you delegate onActivityResult() to it to decode the result obtained from the scanner's activity.

+1
source

Could a simple callback be an alternative? The user places the callback in the static field of your library, and your library will call this callback when needed. The simplest implementation may be as follows:

 YourSdk.staticCallbackField=new OnData() { public void onData(Bundle data) {....}); MyIntent i = new MyIntent(); startActivity(i); 

When the SDK shuts down, it calls a callback:

 staticCallbackField.onData(data) 

User activity will receive Bundle data in the callback instead of onActivityResult.

You should be aware of potential life cycle issues. For example, if an android recreates user activity in a background callback, you should recreate it.

0
source

All Articles