刷新我的片段不像我想的那样工作

Cod*_*joy 2 android android-fragments

我在我的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();
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

当用户在ListFragment视图中单击客户端时调用它:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
       mCurrentSelectedItemIndex = position;    
           showClientDetails(position);
}
Run Code Online (Sandbox Code Playgroud)

这很好用,但是另一个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
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我知道了这条线:

if (details == null || details.getClientIndex() != mCurrentSelectedItemIndex) {

在我的onActivityResult中调用时阻止访问代码块.因此,如果我删除该if声明,那么事情就会变得棘手,而且ft.commit()有一个非常合适的错误并给我错误:

`07-08 16:53:31.783:ERROR/AndroidRuntime(2048):引起:java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作

所以我想我想做的事情并不像它听起来那样干涩和干燥,因为我可以整天在onListItemClicked上做这件事,而且片段一直很好地显示新点击的客户端的详细信息,这对我没有任何意义. .

我甚至试过这个onActivityResult:

//simulate a click event really fast to refresh the client details
showClientDetails(0);
showClientDetails(mCurrentSelectedItemIndex);
Run Code Online (Sandbox Code Playgroud)

什么都不做,是我试图从onActivityResult调用的东西,这不是一个Ui线程或什么不是?

我也在ListFragment的代码中有这个

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}
Run Code Online (Sandbox Code Playgroud)

这是错误抱怨的吗?

PJL*_*PJL 22

另一个FragmentActivity人的行动是执行一项任务,要求Fragment通过调用来保存其状态onSaveInstanceState,以便为重建的新实例做准备.我已经看过这个例子,当我从一个填满整个屏幕的片段中触发一个活动,因为这导致视图与片段分离,状态需要保存等.

你基本上不能commit在之间调用onSaveInstanceState和重新创建的片段的新实例.见提交.

至于解决方案,那么要么重新考虑尝试避免commit被调用,或者commitAllowingStateLoss如果您认为UI可以在用户上意外更改则可以调用.


小智 19

我认为答案是不在onActivityResult中进行任何片段事务.我相信所发生的事情是,当onActivityResult被调用时,它所处的活动尚未恢复并重新启动其片段.使用处理程序将函数调用回发给活动.

handler.post(new Runnable() {
    @Override
    public void run() {
        showClientDetails(mCurrentSelectedItemIndex);
    }
});
Run Code Online (Sandbox Code Playgroud)