Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing an activity from another class

I have a question in Android during creating my google-maps application.

I've one activity as below

public class MapCallActivity extends MapActivity {
    classA class = new classA(this);
    classA.callMethod();
}

The classA is defined as below:

 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?

Any help is appreciated!

like image 326
curlyreggie Avatar asked May 11 '12 23:05

curlyreggie


2 Answers

public class classA{
    public classA(Context context){
        this.myContext = context;
    }

    void callMethod(){
        if(isFileValid){

        }else{
            ((Activity)myContext).finish();
        }
    }
}
like image 105
Changwei Yao Avatar answered Oct 27 '22 04:10

Changwei Yao


What I usually do is to use a receiver within my activity and then trigger a broadcast from class B. Class A gets the broadcast and its onReceive handles the work. Say finish();

like image 20
Ray Avatar answered Oct 27 '22 04:10

Ray