OnActivityResult out of scope

I am trying to create an android project that contains common code that others will use. In this project, I have only POJO and no special classes for Android.

Some functions require calling certain actions and depend on the result. My POJO classes get a reference to the calling activity when it is used, but this happens at runtime, and I do not control the implementation of these actions.

My problem is that with a link to the calling activity, I can run theActivityForResult, but I have no way to add onActivityResult, which may exist in the calling activity, but is not aware of the request that I used.

Now my question is, how do I know from a regular java object when the operation returned? since, as far as I understand, I can only implement onActivityResult for Activity classes.

thanks!

+7
source share
4 answers

You will have quite a few problems with this setting, but here's how you can get started:

You will need to include in the project any actions that do nothing but start the activity from which you want to get the result, and save it in a globally accessible storage (for example, a singleton or static field).

class Pojo { static final ConditionVariable gate = new ConditionVariable(); static int result; int m(Context context) { context.startActivity(new Intent(context, ForwarderActivity.class)); gate.block(); return result; } } class ForwarderActivity extends Activity { private boolean started = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!started) { started = true; startActivityForResult(new Intent("ContactsProvider"), 1); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Pojo.result = resultCode; Pojo.gate.open(); } } 

However, there are a couple of problems. Just like your POJO method cannot be called from the main (UI) thread, since you need to convert the asynchronous call (startActivityForResult ()) to the synchronous (Pojo.m ()) and the activity that you want to receive from will be started mainly stream, so you cannot block it in Pojo.m () ...

Anyway, the code doesn't work, but you can see where to go if you really need to stick with this setting. But you really should try to find other ways to collect data, such as a content provider.

+1
source

I have the same problem while I play with UNITY3D, unity has its activity (unity player), I don’t want to edit it for some reason. but player activity does nothing inside the onActivityResult function. And I need to do something when you select the image picker, I can call "unityPlayer.startActivityForResult" to open the image picker, but NO WAY TO ENCODE MY OWN "onActivityResult".

I think we hope something like this:

OtherActivityClass.onActivityResultListener=function(){My Own CODE}..........OR OtherActivityClass.onActivityResultListener.add(myResultListener)..................

+1
source

Now my question is, how do I know from a regular java object when the operation returned?

Run the POJO operation and set the result.

My POJO classes get a reference to the calling activity when it is used, but this happens at runtime, and I do not control the implementation of these actions.

Then the one who is in “control over the implementation of these actions” will have to activate the POJO operation, providing the result. This is no different from any other callback / listener mechanism.

0
source

Maybe PendingIntent http://developer.android.com/reference/android/app/PendingIntent.html can help you with this. I'm still looking for solutions to my problem, and for me this class looks pretty promising.

Another way could be to create your own abstract class and the onActivityResult method that you want to override. Of course, you will have to rely on JavaDoc and "please call super.onActivityResult" to be able to process the result in your code. But if your class users want some success with your code, they should follow your JavaDoc instructions.

0
source

All Articles