java 关闭另一个班级的活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10559904/
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
Closing an activity from another class
提问by curlyreggie
I have a question in Android during creating my google-maps application.
在创建我的 google-maps 应用程序期间,我在 Android 中有一个问题。
I've one activity as below
我有一项活动如下
public class MapCallActivity extends MapActivity {
classA class = new classA(this);
classA.callMethod();
}
The classA
is defined as below:
的classA
定义如下:
public class classA{
public classA(Context context){
this.myContext = context;
}
void callMethod(){
if (isFileValid) {
<do_something...>;
} else {
<call_the_activity's finish() method>;
}
}
}
Is there a way that I can do <call_the_activity's finish() method>
so that the MapCallActivity
closes?
有没有一种方法,我可以做<call_the_activity's finish() method>
这样的MapCallActivity
关闭?
Any help is appreciated!
任何帮助表示赞赏!
回答by Changwei Yao
public class classA{
public classA(Context context){
this.myContext = context;
}
void callMethod(){
if(isFileValid){
}else{
((Activity)myContext).finish();
}
}
}
回答by Ray
What I usually do is to use a receiver within my activity and then trigger a broadcast from class B. Class Agets the broadcast and its onReceivehandles the work. Say finish();
我通常做的是在我的活动中使用接收器,然后从B 类触发广播。 A 类获取广播,其onReceive处理工作。说完成();