【发布时间】:2011-07-08 17:37:09
【问题描述】:
我的 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: ERROR/AndroidRuntime(2048): Caused by: java.lang.IllegalStateException: Can not perform this action after 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);
}
这是错误所抱怨的吗?
【问题讨论】: