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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:33:26  来源:igfitidea点击:

Closing an activity from another class

javaandroidgoogle-maps

提问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 classAis 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 MapCallActivitycloses?

有没有一种方法,我可以做<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处理工作。说完成();