JoCuTo
JoCuTo

Reputation: 2483

Finishing activity from static method

I would like to do a static method in a class for finishing an activity. I mean something like this : activity A , activity B , activity C, class ActivitiesKiller. Class ActivitiesKiller has a static method for finishing the activity A, B or C.

An example: My runnig class is B and I want to finish activity A, so I will call:

ActivitiesKiller.activityKiller(A);

Is that possible?. Thanks in advanced

I´m doing something like this, but It doesn´t work.

public class ActivitiesKiller {
 //.....

public static void activityKiller(Activity activity){ 
activity.finish();}


}

Upvotes: 1

Views: 2092

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

While it doesn't sound like a good idea, what you would need to do is to make a static reference to the activities on creation, and null them on destruction.

public class ActivityA extends Activity
{
    public static Activity self;

    @Override
    public void onCreate(Bundle saveInstanceState)
    {
       super.onCreate(saveInstanceState);
       self = this;
    }

    @Override
    public void onDestroy()
    {
       super.onDestroy();
       self = null;
    }
}

And then you would need to do the following

public class ActivitiesKiller 
{
    public static void activityKiller(Activity activity)
    { 
        if(activity != null)
        {
            activity.finish();
        }
    }
}

Which you would call as

ActivitiesKiller.activityKiller(ActivityA.self);

Upvotes: 6

Related Questions