Java 使用来自非活动的 startActivityForResult
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use startActivityForResult from non-activity
提问by rayman
I have MainActivity which is an Activity and other class(which is a simple java class), we`ll call it "SimpleClass". now i want to run from that class the command startActivityForResult.
我有 MainActivity,它是一个 Activity 和其他类(这是一个简单的 java 类),我们称之为“SimpleClass”。现在我想从该类运行命令 startActivityForResult。
now i though that i could pass that class(SimpleClass), only MainActivity's context, problem is that, u cant run context.startActivityForResult(...);
现在我虽然我可以通过那个类(SimpleClass),只有 MainActivity 的上下文,问题是,你不能运行 context.startActivityForResult(...);
so the only way making SimpleClass to use 'startActivityForResult; is to pass the reference of MainActivity as an Activity variable to the SimpleClass something like that:
所以让 SimpleClass 使用 'startActivityForResult; 的唯一方法;是将 MainActivity 的引用作为 Activity 变量传递给 SimpleClass,如下所示:
inside the MainActivity class i create the instance of SimpleClass this way:
在 MainActivity 类中,我以这种方式创建 SimpleClass 的实例:
SimpleClass simpleClass=new SimpleClass(MainActivity.this);
now this is how SimpleClass looks like:
现在这是 SimpleClass 的样子:
public Class SimpleClass {
Activity myMainActivity;
public SimpleClass(Activity mainActivity) {
super();
this.myMainActivity=mainActivity;
}
....
public void someMethod(...) {
myMainActivity.startActivityForResult(...);
}
}
now its working, but isnt a proper way of doing this? I`am afraid i could have some memory leaks in the future.
现在它的工作,但不是这样做的正确方法?恐怕我将来可能会出现一些内存泄漏。
thanks. ray.
谢谢。射线。
回答by codinguser
If you need to get the result from the previous Activity, then your calling class needs to be of type Activity.
如果您需要从上一个 Activity 中获取结果,那么您的调用类需要是 Activity 类型。
What is the purpose of you calling Activity.startActivityForResult()
if you never use the result (at least according to the sample code you posted).
Activity.startActivityForResult()
如果您从不使用结果(至少根据您发布的示例代码),那么您调用的目的是什么。
Does myMainActivity
do anything with the result? If so, then just make SimpleClass
a subclass of Activity and handle the result from within SimpleClass
itself.
If myMainActivity
needs the result, then maybe you should refactor the code to start the activity from myMainActivity
.
是否myMainActivity
做了什么结果?如果是这样,那么只需创建SimpleClass
Activity 的子类并从其内部处理结果SimpleClass
。
如果myMainActivity
需要结果,那么也许您应该重构代码以从myMainActivity
.
回答by gtiwari333
Better solution is :
更好的解决方案是:
- Making
SimpleClass
a subclass of yourActivity
class - calling another Activity as
startActivityForResult
- handling the result within
SimpleClass
itself
- 使
SimpleClass
你的子Activity
类 - 将另一个活动称为
startActivityForResult
- 在
SimpleClass
其内部处理结果
回答by Some Noob Student
I don't know if this is good practice or not, but casting a Context object to an Activity object
compiles fine.
我不知道这是否是好的做法,但casting a Context object to an Activity object
编译得很好。
Try this:
尝试这个:
if (mContext instanceof Activity) {
((Activity) mContext).startActivityForResult(...);
} else {
Log.e("mContext should be an instanceof Activity.");
}
This should compile, and the results should be delivered to the actual activity holding the context.
这应该编译,结果应该交付给持有上下文的实际活动。