Boardy
Boardy

Reputation: 36205

Finishing an activity from a standard java class

I am currently working on an android project and I have an activity, lets call it MyActivity and this activity calls a standard Java class called MyClass.

I need MyClass to finish the MyActivity activity but I can't find out how to do this. I thought I might be able to pass the context to the standard java class and call context.finish() but this doesn't appear to be available.

How can I do this, thanks for any help you can offer.

Upvotes: 0

Views: 1533

Answers (4)

Guillermo Blasco
Guillermo Blasco

Reputation: 104

The most secure solution uses listener and a Handler. It is complex, but ensures a non direct call to finish activity.

Your listener:

interface OnWantToCloseListener{
     public void onWantToClose();
}

Class that should close activity.

class MyClass {

    private OnWantToCloseListener listener;

    public void setWantToCloseListener(OnWantToCloseListener listener){
        this.listener = listener;
    }

    private void fireOnWantToClose(){
        if(this.listener != null)
            listener.onWantToClose();
    }
}

When you want to close your activity you must call fireOnWantToClose() method.

public MyActivity extends Activity{

    public void onCreate(){
        final int CLOSE = 1; //number to identify what happens
        MyClass my_class = new MyClass();
        final Handler handler = new Handler(){
             public void handleMessage(Message msg){
                 if(msg.what == CLOSE)
                      MyActivity.this.finish();
             }
        });
        my_class.setOnWantToCloseListener(new OnWantToCloseListener(){
             public void onWantToClose(){
                  handler.sendEmptyMessage(CLOSE);
             }
        });
    }
}

This is secure because Activity is not finished directly by MyClass object, it is finished through a listener that orders a handler to finish activity. Even if you run MyClass object on a second thread this code will works nice.

EDIT: CLOSE var added I forget to declare and initialize this.

Upvotes: 2

Alex Lockwood
Alex Lockwood

Reputation: 83303

If your java class is a nested inner class, you can use:

public class MyActivity extends Activity {

    public static class JavaClass {    

        public void finishActivity() {
            MyActivity.finish();
        }
    }    
}

Otherwise you'll have to pass the java class a Context (i.e. pass it a reference to this, since Activity extends Context) and store it as a private instance variable.

Upvotes: 0

nhaarman
nhaarman

Reputation: 100368

Pass the MyActivity to MyClass as an Activity. From there you can call myActivity.finish();

For example:

private Activity myActivity;

public MyClass(Activity myActivity){
    this.myActivity = myActivity;
}

public void stopMyActivity(){
    myActivity.finish();
}

And in MyActivity:

MyClass myClass = new MyClass(this);

This is risky, because you're holding a reference to an Activity, which can cause memory leaks.

Upvotes: 1

Tyler Treat
Tyler Treat

Reputation: 14988

You can pass the Context, but you will need to cast it to an Activity (or simply pass the Activity itself), although this in general seems like a bad practice.

Upvotes: 2

Related Questions