onActivityResult 超出活动范围
我正在尝试创建一个包含共享代码的android项目,该代码将被其他人使用。 在这个项目中我只有 POJO 而没有 android 特定的类。
某些功能需要调用某些活动并取决于结果。 我的 POJO 类在使用时获取调用活动的引用,但这是在运行时发生的,我无法控制这些活动的实现。
我的问题是,通过引用调用活动,我可以startActivityForResult,但我无法添加onActivityResult,它可能存在于调用活动中,但不知道我使用的请求代码。
那么我的问题是如何从常规 java 对象中知道活动何时返回?因为,据我了解,我只能在 Activity 类上实现 onActivityResult 。
谢谢!
I'm trying to create an android project that contains shared code which will be used by others.
In this project I only have POJO and no android specific classes.
Some of the functionality requires calling some activities and is depended on the result.
My POJO classes get reference to the calling activity when used, but that's happening at run time and I have no control over the implementation of those activities.
My problem is that with the reference of the calling activity I can startActivityForResult but I have no way of adding the onActivityResult, which might exists in the calling activity but is not aware of the requestCode I used.
My question then is how do I know, from within a regular java object when the activity has returned? since, as far as I understand I can only implement onActivityResult on Activity classes.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此设置会遇到很多问题,但这就是您可能想要开始的方式:
您必须在项目中包含一个活动,该活动除了启动您想要从中获取结果的活动并将其存储之外,什么也不做。在全局可访问的存储中(例如单例或静态字段)。
但也存在一些问题。就像你的 POJO 的方法不能从主(UI)线程调用一样,因为你需要将异步调用(startActivityForResult())转换为同步调用(Pojo.m())以及你想要从中接收信息的活动将在主线程上启动,因此您不能在 Pojo.m() 中阻止它...
无论如何,该代码不起作用,但如果您确实必须坚持此设置,您可以看到该走哪条路。但您确实应该尝试想出一些其他方式来获取数据,例如内容提供商。
You will have quite a few problems with this setup, but this is how you might want to get started:
You will have to include an activity in your project which does nothing else than starting the activity you want to get the result from and stores it in a globally accessible storage (e.g. a singleton or a static field).
There are a couple of problems, though. Like your POJO's method can't be called from the main (UI) thread, because you need to convert an asynchronous call (startActivityForResult()) to a synchronous one (Pojo.m()) and the activity you want to receive info from will be started on the main thread, so you can't block it in Pojo.m()...
Anyway, the code does not work, but you can see which way to go if you really have to stick with this setup. But you should really try to come up with some other means of fetching the data, like a content provider.
我在玩 UNITY3D 时遇到了同样的问题,unity 有它自己的 Activity(unity 播放器),出于某种原因我不想编辑它。但玩家活动在“onActivityResult”函数内不执行任何操作。当访问图像选择器时我有事情要做,我可以调用“unityPlayer.startActivityForResult”来打开图像选择器,但无法编写自己的“onActivityResult”。
我认为我们希望是这样的:
OtherActivityClass.onActivityResultListener=function(){My Own CODE}........................OR
OtherActivityClass.onActivityResultListener.add(myResultListener)........................
i have the same problem while i play with UNITY3D,the unity have it's own Activity(the unity player),i don't wanna edit it for some reason. but the player activity do nothing inside the "onActivityResult" function. And i have something to do when access image picker,i can call "unityPlayer.startActivityForResult" to open image picker,but NO WAY TO CODE MY OWN "onActivityResult".
i think what we hope is something like this:
OtherActivityClass.onActivityResultListener=function(){My Own CODE}..........OR
OtherActivityClass.onActivityResultListener.add(myResultListener)..................
让活动调用 POJO,提供结果。
然后,无论谁“控制这些活动的实施”,都需要让活动调用 POJO,提供结果。这与任何其他回调/侦听器机制没有什么不同。
Have the activity call the POJO, supplying the result.
Then whoever is in "control over the implementation of those activities" will need to have the activity call the POJO, supplying the result. This is no different than any other callback/listener mechanism.
也许 PendingIntent http://developer.android.com/reference/android/app/PendingIntent .html 可以帮助您。我仍在寻找解决我的问题的方法,对我来说,这门课看起来很有前途。
另一种方法可能是使您自己的类抽象并具有需要重写的方法
onActivityResult
。当然,您必须依赖 JavaDoc 和“请调用 super.onActivityResult”才能在代码中处理结果。但是,如果您的类的用户希望使用您的代码取得一些成功,他们应该遵循您的 JavaDoc 说明。Maybe PendingIntent http://developer.android.com/reference/android/app/PendingIntent.html can help you with that. I'm still looking around for a solution to my problem and for me, this class looks quite promising.
Another way might be to make your own class abstract and have a method
onActivityResult
that is required to be overridden. Of course, you would have to rely on JavaDoc and "please call super.onActivityResult" to be able to process the result in your code. But if the users of your class want to have some success with your code they should follow your JavaDoc instructions.与 Szabolcs Berecz 所建议的类似,这是可能的。没有完美的解决方案,但以下是可能的:
我已在应用程序设置库中进行了设置,某些设置确实启动了系统意图(例如选择图像)并需要等待其结果即使屏幕旋转后,这也可以工作,无需库用户的任何代码调整。
Similar to what Szabolcs Berecz suggests, it is possible. There is no beautiful solution, but following is possible:
startActivityForResult
and waiting for its result is non blocking in generalI've set this up in a library for app settings and some settings do start a system intent (e.g. select an image) and need to wait for their result and this works even after screen rotation without the need of any code adjustments by the libraries user.