Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close the current activity when you only have a reference to Context

If I have a reference to Context, is it possible to finish the current activity?

I don't have the reference to current activity.

like image 208
Buda Gavril Avatar asked Mar 21 '11 11:03

Buda Gavril


People also ask

How do you finish current activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

Can context be cast to activity?

The Best Answer is getApplicationContext(); Wherever you are passing it pass this or ActivityName. this instead. you get this exception because you can't cast the Application to Activity since Application is not a sub-class of Activity .


1 Answers

yes, with a cast:

((Activity) ctx).finish(); 
like image 103
2red13 Avatar answered Nov 09 '22 17:11

2red13