【问题标题】:Fragment is recreate on back press from other Fragment片段是在其他片段的背压上重新创建的
【发布时间】:2014-07-28 08:04:13
【问题描述】:

我在片段方面遇到问题。 在我的场景中,

有两个片段与 FragmentActivity 相关联。 在 FragmentActivity 中有一个容器布局(Frame Layout),所有的 Fragment 都会被替换掉。

public void replaceFragment(Fragment fragmentClass) {
        String selectedFragment = fragmentClass.getClass().getName();


            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction
                    .replace(R.id.content_frame, fragmentClass);            
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();           

    }

第一次,我设置了一个列表类型片段(片段 A),在其中从 Web 服务获取数据并在列表视图上填充。我从 onCreateView() 方法执行 AsyncTask。

在 MainActivity:onCreate

SherlockFragment fragment = new FragmentA();
replaceFragment(fragment);

在Fragment A的列表项点击时,Fragment A会回调activity方法,将其替换为详情类型的Fragment Fragment B。

@Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        callback = (ICallBack) activity;

    }


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /*View rootView = inflater.inflate(R.layout.fragment_locate, container,
                false);*/
        View rootView = inflater.inflate(R.layout.fragment_a, container,
                false);

        ListView list = (ListView) rootView
                .findViewById(R.id.listView);
        adapter = new MyListAdapter();
        list.setAdapter(adapter);
        list
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent,
                            View convertView, int position, long id) {
                                SherlockFragment fragment = new SalonDetailFragment();
                                callback.replaceFragment(fragment);
                    }
                });

        ListDataTask task = new  ListDataTask();
        task.execute();

        return rootView;
    }

    class ListDataTask extends AsynTask<Void,Void,List<Data>>{

    public Void doInBackground(Void parems){

        List<Data> = getDataFromServer();
    }
    onPostExecute(List<Data> data){
        adapter.addAllData(data);
        adapter.notifyDataSetChanged();
    }

    }

当我按下返回按钮时,从 Fragment B 然后应用程序将显示 Fragment A 但它再次执行 Asynctask 并获取数据以填充列表视图。

所以我需要知道,如何像Activity一样保持Fragment的可渗透状态。

从其他活动回来后有什么办法不创建片段

看看我的伪代码。

【问题讨论】:

标签: android android-fragments android-fragmentactivity savestate


【解决方案1】:

我得到了解决方案。简单....检查rootview的null值

public class FragmentA extends Fragment {
    View _rootView;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (_rootView == null) {
            // Inflate the layout for this fragment 
            _rootView = inflater.inflate(R.layout.fragment_a, container, false);
            // Find and setup subviews 
            _listView = (ListView)_rootView.findViewById(R.id.listView);
            ... 
        } else { 
            // Do not inflate the layout again. 
            // The returned View of onCreateView will be added into the fragment. 
            // However it is not allowed to be added twice even if the parent is same. 
            // So we must remove _rootView from the existing parent view group 
            // (it will be added back). 
            ((ViewGroup)_rootView.getParent()).removeView(_rootView);
        } 
        return _rootView

【讨论】:

  • 我们应该在这个 Fragment 中调用 API 的位置,因为正在命中背压 API。
【解决方案2】:

我有同样的问题,通过更换解决了

 fragmentTransaction
                    .replace(R.id.content_frame, fragmentClass); 
to
 fragmentTransaction
                    .add(R.id.content_frame, fragmentClass); 

Replace 总是会在后按时创建新实例,而 Add 只是在 Stack 中添加一个新片段。更多信息请查看link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多