I have two activities A and B. B is a transparent pass through activity, and A is seen. I want to kill B by pressing a button A.
Here\'s what I\'ve tried so far:
Create static Class variable to save the instance: static SampleActivity sampleActivity;
On Create of first Activity save the Instance, like this: incidenteActivity = this;
Create a static method to get the instance:
public static SampleActivity getInstance(){
return sampleActivity;
}
wherever you want call:
SampleActivity.getInstance().finish();
it really works, regards,
I found a nice way to finish one activity from another, it is similar to what Lumis did. So if you want to close ActivityA from ActivityB you can do this:
In ActivityA do:
className = this.getClass().getName();
and pass it on to AvtivityB. Then in ActivityB do:
((Activity) Class.forName(className).newInstance()).finish();
You can put a string with the name of your class into className yourself, but it needs to be a full name with package too.
You can stop the other activity by calling Activity.finish(). Alternatively, you can use Activity.finishActivity() to finish an activity started by startActivityForResult().
You could try to directly kill an activity by calling a static method in that activity:
Activity A should have a variable
static ActivityA activityA;
In onCreate state:
activityA = this;
and add this method:
public static ActivityA getInstance(){
return activityA;
}
In activity B, call the function getInstance()
ActivityA.getInstance().finish();