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

前端 未结 7 1637
暗喜
暗喜 2020-12-12 23:47

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

I don\'t have the reference to current activity.

7条回答
  •  时光取名叫无心
    2020-12-12 23:52

    If you start the activity using:

    startActivityForResult(i, 1);
    

    you can call finishActivity(1) to finish any activities started with that request code, like this:

    ((Activity)getContext()).finishActivity(1);
    

    In my case I need to use one in a handler postDelayed. Using this you can be sure of which activity you are finishing. Hope it helps!

提交回复
热议问题