在我的ListFragment中有一个很好的方法,我调用它来填充我的其他片段的细节:
private void showClientDetails(int pos) {
myCursor.moveToPosition(pos);
int clientId = myCursor.getInt(0);
if(mIsTablet) {
// Set the list item as checked
getListView().setItemChecked(mCurrentSelectedItemIndex, true);
// Get the fragment instance
ClientDetails details = (ClientDetails) getFragmentManager().findFragmentById(R.id.client_details);
// Is the current visible recipe the same as the clicked? If so, there is no need to update
if (details == null || details.getClientIndex() != mCurrentSelectedItemIndex) {
// Make new fragment instance to show the recipe
details = ClientDetails.newInstance(mCurrentSelectedItemIndex, clientId, mIsTablet);
// Replace the old fragment with the new one
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.client_details, details);
// Use a fade animation. This makes it clear that this is not a new "layer"
// above the current, but a replacement
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}当用户单击ListFragment视图中的客户端时,将调用该函数:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCurrentSelectedItemIndex = position;
showClientDetails(position);
}这很好用,但是另一个FragmentActivity可以更改它显示的数据,所以我认为这是可行的:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//update the client list incase a new one is added or the name changes
if(requestCode==1899)
{
myCursor.requery();
theClients.notifyDataSetChanged();
showClientDetails(mCurrentSelectedItemIndex); //now if client was edited update their details in the details fragment
}
}现在我知道这句话了:
if (details == null || details.getClientIndex() != mCurrentSelectedItemIndex) {
防止在onActivityResult中调用代码块时到达该代码块。因此,如果我删除了if语句,事情就会变得异常,ft.commit()就会突然出现问题,并给出错误:
`07-08 16:53:31.783:错误/AndroidRuntime(2048):由: java.lang.IllegalStateException: onSaveInstanceState后无法执行此操作
所以我猜我想要做的并不像听起来那么简单,这对我来说没有任何意义,因为我可以整天做onListItemClicked,而且片段很好地显示了新点击的客户端的详细信息……
我甚至在我的onActivityResult中尝试过
//simulate a click event really fast to refresh the client details
showClientDetails(0);
showClientDetails(mCurrentSelectedItemIndex);这没什么用,我是想从onActivityResult中调用一些不是UI线程的东西,还是什么都不是?
在我的ListFragment代码中也有这一点
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}这就是错误所抱怨的吗?
发布于 2011-07-11 14:52:28
另一个FragmentActivity的动作是执行一个任务,它需要Fragment通过调用onSaveInstanceState来保存它的状态,以便为新的实例重建做准备。例如,当我从充满整个屏幕的片段中触发一个活动时,我就看到过这种情况,因为这导致视图与片段分离,状态需要保存等等。
基本上不能在onSaveInstanceState和正在重新创建的片段的新实例之间调用commit。参见commit。
至于解决方案,要么重新考虑尝试避免在调用commit时调用它,要么调用commitAllowingStateLoss,如果您认为UI在用户身上意外更改是可以接受的。
发布于 2011-11-02 07:50:07
我认为答案是在onActivityResult中不做任何碎片事务。我相信发生的情况是,当onActivityResult被调用时,它所在的活动尚未恢复并重新启动其片段。使用处理程序将函数调用回发到活动。
handler.post(new Runnable() {
@Override
public void run() {
showClientDetails(mCurrentSelectedItemIndex);
}
});发布于 2012-11-30 23:55:18
你可以做另一件事,虽然不是很好,但很管用。
Intent mIntent =新Intent(getApplicationContext(),YouClass.class);
startActivity(mIntent);
https://stackoverflow.com/questions/6628215
复制相似问题