Check whether activity is active

前端 未结 8 2287
眼角桃花
眼角桃花 2020-11-27 04:47

I\'m having a problem with a listener in a certain activity.

The problem is that this listener contains an alert.show(); which can be called after we try to push a

8条回答
  •  不知归路
    2020-11-27 05:05

    Background thread after finishing their networking tasks invokes callback onSuccess()/onFailure() on the main thread. And if at that time, the activity which initiated this background thread task is not in the foreground and you try to use getActivity() in either onSuccess()/onFailure(), it will give you the exception. So try to add this check before doing any UI operation.

    if(!((Activity) context).isFinishing())
    {
        //show alert
    }
    

提交回复
热议问题