刷新我的片段不工作像我想象的要 [英] Refreshing my fragment not working like I thought it should

查看:94
本文介绍了刷新我的片段不工作像我想象的要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ListFragment这个漂亮的方法,我打电话来填充我的另一个片段的细节:

I have this nice method in my ListFragment I call to fill out the details of my other fragment:

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认为这就是所谓的:

It is called when the user clicks on a client in the ListFragment view:

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

这个伟大的工程,但后来又FragmentActivity可以更改数据,这显示,所以我认为这会工作:

This works great, but then another FragmentActivity can change the data that this displays so I thought this would work:

@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
    }
}

现在我知道了行:

如果(详细信息== NULL || details.getClientIndex()!= mCurrentSelectedItemIndex){

prevents的code块被达到时,它在我的onActivityResult调用。所以,如果我删除如果语句,那么事情吓坏了与ft.commit()有嘘声配合和给我的错误:

Prevents the block of code being reached when its called in my onActivityResult. So if I remove that if statement, then things freak out and the ft.commit() has a hissy fit and gives me the error:

`07-08 16:53:31.783:ERROR / AndroidRuntime(2048):java.lang.IllegalStateException:产生的原因的onSaveInstanceState后无法执行此操作

`07-08 16:53:31.783: ERROR/AndroidRuntime(2048): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

所以我猜什么,我试图做的是不是添油加醋因为它的声音,这是没有意义的我,因为我能做到这一点onListItemClicked整天的片段显示新点击客户端的细节不断很好...

So I guess what I am trying to do isn't as cut and dry as it sounds, it makes no sense to me since I can do that onListItemClicked all day and the fragment displays the details of the newly clicked client constantly very well...

我甚至在尝试这样做我的 onActivityResult

I even tried this in my onActivityResult:

//simulate a click event really fast to refresh the client details
showClientDetails(0);
showClientDetails(mCurrentSelectedItemIndex);

这是什么也不做,是不是我试着打电话给一些从onActivityResult这是不是一个UI线程还是什么呢?

that does nothing, is it im trying to call something from the onActivityResult which isn't a Ui thread or what not?

我做我的$ C $的ListFragment了C,以及有此

I do have this in my code of the ListFragment as well

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}

这是什么错误是抱怨?

Is this what the error is complaining about?

推荐答案

其他 FragmentActivity 的行动正在执行这就要求片段任务通过在preparation一个新的实例调用的onSaveInstanceState 保存其状态正在重建。我已经看到了这个例如,当我发射了从一个片段充斥着整个屏幕的活动,因为这导致了查看从片段被分离,国家需要保存等。

The other FragmentActivity's action is performing a task which requires the Fragment to save its state via a call to onSaveInstanceState in preparation for a new instance being reconstructed. I've seen this for example when I was firing off an activity from a fragment which filled the entire screen as this resulted in the view being detached from the fragment, state needing to be saved etc.

您根本不能称之为提交的onSaveInstanceState 和片段的新实例被创建。请参阅<一href="http://developer.android.com/reference/android/app/FragmentTransaction.html#commit%28%29">commit.

You basically cannot call commit between onSaveInstanceState and the new instance of the fragment being recreated. See commit.

至于解决方案,然后要么重新思考,试图避免提交被调用时,它或者您也可以致电 commitAllowingStateLoss ,如果你认为这是正常的用户界面对用户意外更改。

As for the solution, then either re-think to try and avoid the commit being called when it is or alternatively call commitAllowingStateLoss if you think it's OK for the UI to change unexpectedly on the user.

这篇关于刷新我的片段不工作像我想象的要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆