我正在创建一个Activity
与Service
通信通过 POST
从互联网下载一些数据方法。为此,我使用 Messenger
。这是我的代码,让您更清楚:
我的onCreated()
方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
CommentsHandler commentsHandler = new CommentsHandler(this, savedInstanceState);
Messenger messenger = new Messenger(commentsHandler);
Intent serviceIntent = new Intent(this, WindowService.class);
serviceIntent.putExtra("messenger", messenger);
serviceIntent.putExtra("state", 888);
serviceIntent.putExtra("number", getIntent().getStringExtra("number"));
startService(serviceIntent);
}
我的Service
中的代码的线程将结果数据发布到 Activity
通过Messenger
对象:
/** ... **/
Messenger messenger = intent.getParcelableExtra("messenger");
/** ... **/
Message resultMsg = this.obtainMessage();
resultMsg.obj = jParser.getArrayList(); //This is an ArrayList of my downloaded data.
messenger.send(resultMsg);
Activity
中的代码处理 Message
来自Service
:
public static class CommentsHandler extends Handler {
Bundle mSavedInstanceState;
ActionBarActivity activity;
public CommentsHandler(Activity a, Bundle savedInstanceState) {
activity = (ActionBarActivity) a;
mSavedInstanceState = savedInstanceState;
}
@Override
public void handleMessage(Message msg) {
comments = (ArrayList<HashMap<String, String>>) msg.obj;
if (mSavedInstanceState == null && msg.arg1 != 793) {
activity.getSupportFragmentManager().beginTransaction()
.add(R.id.container, new CommentsFragment()).commit();
} else if (msg.arg1 == 793) { //793 is my preferred code to determine
//if the internet connection could not be
//established when the Service was trying
//to download the data.
activity.finish();
}
}
}
问题是:如果我打开Activity
并在下载数据之前将其关闭,此代码.add(R.id.container, new CommentsFragment()).commit();
给我错误 Can not perform this action after onSaveInstanceState
,因为此代码仅在我的 Service
中的数据之后执行通过 Messenger
处理并发送对象,但当时Activity
已被用户关闭,因此 Fragment
无法添加。如何解决这个问题?如何检查 Activity
是否在添加 Fragment
之前未关闭/正在关闭?或者,更好的是,如何停止在 Activity
上运行该代码的线程的onDestroy()
方法,因此如果 Activity
则不会执行它关闭了?提前致谢!
最佳答案
在您的 Activity 中,您应该创建一个 boolean 值来检查 Activity 是否可见:
public ActionBarActivity extends Activity {
private boolean isActivityVisible = false;
@Override
protected void onResume(){
isActivityVisible = true;
}
@Override
protected void onPause(){
isActivityVisible = false;
}
public boolean isVisible(){
return this.isActivityVisible;
}
}
然后修改 Handler 类定义:
public static class CommentsHandler extends Handler {
Bundle mSavedInstanceState;
ActionBarActivity activity;
public CommentsHandler(Activity a, Bundle savedInstanceState) {
activity = (ActionBarActivity) a;
mSavedInstanceState = savedInstanceState;
}
@Override
public void handleMessage(Message msg) {
// here you check if your activity is no longer visible and then break up
if(activity == null || !activity.isVisible())
return;
comments = (ArrayList<HashMap<String, String>>) msg.obj;
if (mSavedInstanceState == null && msg.arg1 != 793) {
activity.getSupportFragmentManager().beginTransaction()
.add(R.id.container, new CommentsFragment()).commit();
} else if (msg.arg1 == 793) { //793 is my preferred code to determine
//if the internet connection could not be
//established when the Service was trying
//to download the data.
activity.finish();
}
}
}
关于java - 如何防止在 Activity 关闭时添加 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849664/