在一个程序中使用fragment+异步更新界面,今天出现发现异常,记录解决办法。
异常:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState;
分析:仔细查看后发现,是由于异步线程结束后,要弹出对话框提示,但是这时候fragment已经进入后台,所以抛出这个异常;
解决办法:在线程运行结束后,需要弹出对话框的时候,先做一个判断isResumed()。google给的文档中是这样说的:
public final boolean isResumed ()
Return true if the fragment is in the resumed state. This is true for the duration of onResume()
and onPause()
as well.