Android 刷新我的片段没有像我想的那样工作
在我的ListFragment中有一个很好的方法,我调用它来填写我的其他片段的详细信息:Android 刷新我的片段没有像我想的那样工作,android,android-fragments,Android,Android Fragments,在我的ListFragment中有一个很好的方法,我调用它来填写我的其他片段的详细信息: private void showClientDetails(int pos) { myCursor.moveToPosition(pos); int clientId = myCursor.getInt(0); if(mIsTablet) { // Set the list item
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);
}
这非常有效,但另一个碎片活动可以更改显示的数据,因此我认为这会起作用:
@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之后无法执行此操作
所以我想我尝试做的事情并不像听起来那么简单,这对我来说毫无意义,因为我只能整天点击istitemclicked,而片段总是很好地显示新点击的客户端的详细信息
我甚至在我的活动结果中尝试了这个:
//simulate a click event really fast to refresh the client details
showClientDetails(0);
showClientDetails(mCurrentSelectedItemIndex);
这没有任何作用,是不是我试图从非Ui线程的onActivityResult调用什么
我的ListFragment代码中也有这个
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}
这就是错误所抱怨的吗?另一个
FragmentActivity
的操作正在执行一项任务,该任务要求片段通过调用onSaveInstanceState
保存其状态,以备重建新实例。例如,我在m是一个片段,它填充了整个屏幕,因为这导致视图与片段分离,需要保存的状态等
基本上不能在onSaveInstanceState
和正在重新创建的片段的新实例之间调用commit
。请参阅
至于解决方案,那么要么重新考虑尝试避免在调用时调用commit
,要么调用commitAllowingStateLoss
,如果您认为用户界面可以意外更改的话。我想答案是在onActivityResult中不执行任何片段事务。我相信会发生什么当onActivityResult被调用时,它所在的活动尚未恢复并重新启动其片段。
使用处理程序将函数调用发回活动
handler.post(new Runnable() {
@Override
public void run() {
showClientDetails(mCurrentSelectedItemIndex);
}
});
你可以做另一件不太好但很有效的事情
- 完成(你的活动)
- 通过以下方式实现同一类的目的:
Intent minent=newintent(getApplicationContext(),YouClass.class)
星触觉
感谢您的回复。显示的客户端是从“编辑客户端”窗口更新的,因此我认为这并非意外,而且我认为commitAllowingStateLoss也可以。我使用过commitAllowingStateLoss,但在方向更改时显示了警报对话框。我想知道,如果您的应用程序位于后台,并且仍处于运行状态,这会起作用吗后台,何时调用onActivityResult?